3
3
4
4
public class CombinationsOfSumK {
5
5
public List <List <Integer >> combinationsOfSumK (int [] nums , int target ) {
6
- List <List <Integer >> result = new ArrayList <>();
7
- dfs (new ArrayList <>(), 0 , nums , target , result );
8
- return result ;
6
+ List <List <Integer >> res = new ArrayList <>();
7
+ dfs (new ArrayList <>(), 0 , nums , target , res );
8
+ return res ;
9
9
}
10
10
11
- private void dfs (List <Integer > combination , int startIndex , int [] nums , int target , List <List <Integer >> result ) {
11
+ private void dfs (List <Integer > combination , int startIndex , int [] nums , int target , List <List <Integer >> res ) {
12
12
// Termination condition: If the target is equal to 0, we found a combination
13
13
// that sums to 'k'.
14
14
if (target == 0 ) {
15
- result .add (new ArrayList <>(combination ));
15
+ res .add (new ArrayList <>(combination ));
16
16
return ;
17
17
}
18
18
// Termination condition: If the target is less than 0, no more valid
@@ -25,7 +25,7 @@ private void dfs(List<Integer> combination, int startIndex, int[] nums, int targ
25
25
// Add the current number to create a new combination.
26
26
combination .add (nums [i ]);
27
27
// Recursively explore all paths that branch from this new combination.
28
- dfs (combination , i , nums , target - nums [i ], result );
28
+ dfs (combination , i , nums , target - nums [i ], res );
29
29
// Backtrack by removing the number we just added.
30
30
combination .remove (combination .size () - 1 );
31
31
}
0 commit comments