class Solution {
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> combine(int n, int k) {
List<Integer> list = new ArrayList<>();
helper(k, n, list, 1);
return res;
}
private void helper(int k, int n, List<Integer> list, int index){
if(list.size() == k){
res.add(new ArrayList<>(list));
return;
}
for(int i=index; i<=n; i++){
list.add(i);
helper(k, n, list, index+1);
list.remove(list.size()-1);
}
return;
}
}
When I do this, the output is as follows: [[1,2],[1,3],[1,4],[2,2],[2,3],[2,4],[3,2],[3,3],[3,4],[4,2],[4,3],[4,4]]. And the expected output is: [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]].
But when I change this line helper(k, n, list, index+1); to helper(k, n, list, i+1);, the output becomes correct.
I want to know the difference between the two, but so far I'm not getting an answer (isn't it supposed to be the same value every time for both index+1 and i+1?).