forked from varunu28/LeetCode-Java-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinations.java
More file actions
27 lines (22 loc) · 729 Bytes
/
Combinations.java
File metadata and controls
27 lines (22 loc) · 729 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public List<List<Integer>> combine(int n, int k) {
List<Integer> temp = new ArrayList<>();
List<List<Integer>> ans = new ArrayList<>();
helper(n, 1, temp, ans, k);
return ans;
}
private void helper(int n, int start, List<Integer> temp, List<List<Integer>> ans, int len) {
if (temp.size() == len) {
ans.add(new ArrayList<>(temp));
return;
}
for (int i=start; i<=n; i++) {
// Choose
temp.add(i);
// Explore
helper(n, i+1, temp, ans, len);
// Un-choose
temp.remove(temp.size() - 1);
}
}
}