-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCombinationSum2.java
More file actions
53 lines (45 loc) · 1.76 KB
/
CombinationSum2.java
File metadata and controls
53 lines (45 loc) · 1.76 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
package com.company;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CombinationSum2 {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> mCombinationSum2Lists = new ArrayList<>();
if (candidates == null || candidates.length == 0)
return mCombinationSum2Lists;
Arrays.sort(candidates);
helper(candidates, mCombinationSum2Lists, new ArrayList<>(), target, 0);
if (mCombinationSum2Lists == null || mCombinationSum2Lists.size() == 0)
return mCombinationSum2Lists;
return mCombinationSum2Lists;
}
public void helper(int[] candidates,
List<List<Integer>> mCombinationSum2Lists,
ArrayList<Integer> mList,
int target, int start) {
if (target < 0) return;
if (target == 0) {
mCombinationSum2Lists.add(new ArrayList<>(mList));
return;
}
for (int i = start; i < candidates.length; i++) {
if (candidates[i] > target)
return;
if (i > start && candidates[i] == candidates[i - 1])
continue;
mList.add(candidates[i]);
helper(candidates, mCombinationSum2Lists, mList, target - candidates[i], i + 1);
mList.remove(mList.size() - 1);
}
}
@Test
public void test() {
List<List<Integer>> lists =
combinationSum2(new int[]{10, 1, 2, 7, 6, 1, 5}, 8);
for (List<Integer> list : lists) {
for (Integer a : list) System.out.print(a + " ");
System.out.println();
}
}
}