@@ -24,27 +24,27 @@ All numbers (including target) will be positive integers.
24
24
public class _40 {
25
25
26
26
public static class Solution1 {
27
- public List <List <Integer >> combinationSum2 (int [] candidates , int target ) {
28
- List <List <Integer >> result = new ArrayList ();
29
- Arrays .sort (candidates );
30
- backtracking (candidates , target , 0 , new ArrayList (), result );
31
- return result ;
32
- }
27
+ public List <List <Integer >> combinationSum2 (int [] candidates , int target ) {
28
+ List <List <Integer >> result = new ArrayList ();
29
+ Arrays .sort (candidates );
30
+ backtracking (candidates , 0 , result , target , new ArrayList ());
31
+ return result ;
32
+ }
33
33
34
- void backtracking (int [] candidates , int target , int start , List <Integer > curr , List <List <Integer >> result ) {
35
- if (target > 0 ) {
36
- for (int i = start ; i < candidates .length && target >= candidates [i ]; i ++) {
37
- if (i > start && candidates [i ] == candidates [i - 1 ]) {
38
- continue ;//skip duplicates, this is one difference from Combination Sum I
39
- }
40
- curr .add (candidates [i ]);
41
- backtracking (candidates , target - candidates [i ], i + 1 , curr , result );//i+1 is the other difference from Combination Sum I
42
- curr .remove (curr .size () - 1 );
43
- }
44
- } else if (target == 0 ) {
45
- result .add (new ArrayList (curr ));
34
+ void backtracking (int [] candidates , int start , List <List <Integer >> result , int target ,
35
+ List <Integer > curr ) {
36
+ if (target > 0 ) {
37
+ for (int i = start ; i < candidates .length ; i ++) {
38
+ if (candidates [i ] > target || (i > start && candidates [i - 1 ] == candidates [i ])) {
39
+ continue ;
46
40
}
41
+ curr .add (candidates [i ]);
42
+ backtracking (candidates , i + 1 , result , target - candidates [i ], curr );
43
+ curr .remove (curr .size () - 1 );
44
+ }
45
+ } else if (target == 0 ) {
46
+ result .add (new ArrayList (curr ));
47
47
}
48
+ }
48
49
}
49
-
50
- }
50
+ }
0 commit comments