Skip to content

Commit cef4653

Browse files
39. Combination Sum (java)
1 parent 0c366c0 commit cef4653

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
3+
List<List<Integer>> result = new ArrayList<>();
4+
Arrays.sort(candidates);
5+
combinationSum(candidates,target,candidates.length-1, new ArrayList<>(),result);
6+
return result;
7+
}
8+
9+
private void combinationSum(int[] candidates, int target,int length, List<Integer> integers,
10+
List<List<Integer>> result) {
11+
List<Integer> list;
12+
for (int i = length; i >= 0; i--) {
13+
int nc = candidates[i];
14+
if (nc>target) continue;
15+
list = new ArrayList<>(integers);
16+
list.add(nc);
17+
if (nc==target) result.add(list);
18+
else combinationSum(candidates, target - nc, i, list,result);
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)