|
1 | 1 | classSolution { |
2 | | -publicList<List<Integer>>combinationSum(int[]candidates,inttarget) { |
3 | | -List<List<Integer>>result =newArrayList<>(); |
4 | | -helper(candidates,0,target,newArrayList<>(),result); |
5 | | -returnresult; |
6 | | - } |
| 2 | +publicList<List<Integer>>combinationSum(int[]candidates,inttarget) { |
| 3 | +List<List<Integer>>result =newArrayList<>(); |
| 4 | +helper(candidates,target,newArrayList<>(),result,0); |
| 5 | +returnresult; |
| 6 | +} |
7 | 7 |
|
8 | | -privatevoidhelper(int[]candidates,intidx,inttarget,List<Integer>currCombination, |
9 | | -List<List<Integer>>result) { |
10 | | -if (target ==0) { |
11 | | -result.add(newArrayList<>(currCombination)); |
12 | | - } |
13 | | -if (target >0 &&idx <candidates.length) { |
14 | | -for (inti =idx;i <candidates.length;i++) { |
15 | | -currCombination.add(candidates[i]); |
16 | | -helper(candidates,i,target -candidates[i],currCombination,result); |
17 | | -currCombination.remove(currCombination.size() -1); |
18 | | - } |
| 8 | +privatevoidhelper(int[]candidates,inttarget,List<Integer>combination,List<List<Integer>>result,intidx) { |
| 9 | +if (target <0 ||idx ==candidates.length) { |
| 10 | +return; |
| 11 | + } |
| 12 | +if (target ==0) { |
| 13 | +result.add(combination); |
| 14 | +return; |
| 15 | + } |
| 16 | +for (inti =idx;i <candidates.length;i++) { |
| 17 | +combination.add(candidates[i]); |
| 18 | +helper(candidates,target -candidates[i],newArrayList<>(combination),result,i); |
| 19 | +combination.remove(combination.size() -1); |
| 20 | + } |
19 | 21 | } |
20 | | - } |
21 | 22 | } |