Skip to content

Commit de9981c

Browse files
committed
Time: 3 ms (36.45%) | Memory: 43.9 MB (60.85%) - LeetSync
1 parent a805ed1 commit de9981c

File tree

1 file changed

+20
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)