-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSumTest.java
More file actions
87 lines (73 loc) · 1.99 KB
/
NSumTest.java
File metadata and controls
87 lines (73 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package nSum;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
public class NSumTest {
@Test
public void testNSum() {
int[][] nums = new int[][]{
{},
{1,5,3,2},
{1,1,2,2,2,3,3,4},
{2,2,2,2,2,2,2,2,2,2}
};
int[][][] expect = new int[][][]{
{{}},
{{1,2,3}},
{{1,3,4},{2,2,4}},
{{2,2,2}}
};
int[] target = new int[]{
0,
6,
8,
6
};
NSum solution = new NSum();
for(int i=0;i<nums.length;i++) {
ArrayList<ArrayList<Integer>> res = solution.nSum(nums[i], target[i], 3);
assertNSum(res, expect[i], 3);
}
}
@Test
public void testNSum2() {
int[][] nums = new int[][]{
{},
{1,5,3,2},
{1,1,2,2,2,3,3,4,4},
{2,2,2,2,2,2,2,2,2,2}
};
int[][][] expect = new int[][][]{
{{}},
{{1,2,3}},
{{1,1,4,4},{2,2,3,3}},
{{2,2,2,2,2,2}}
};
int[] target = new int[]{
0,
10,
10,
12
};
int []N = new int[] {
0,
3,
4,
6
};
NSum solution = new NSum();
for(int i=0;i<nums.length;i++) {
ArrayList<ArrayList<Integer>> res = solution.nSum(nums[i], target[i], N[i]);
assertNSum(res, expect[i], N[i]);
}
}
private void assertNSum(ArrayList<ArrayList<Integer>>res, int[][] expect,int n) {
for(int i=0;i<res.size();i++) {
int[] temp = new int[n];
for(int j=0;j<res.get(i).size();j++) {
temp[j] = res.get(i).get(j);
}
Assert.assertArrayEquals(temp, expect[i]);
}
}
}