LeetCode 77. 组合
原题链接
中等
作者:
LangB
,
2020-11-03 11:40:27
,
所有人可见
,
阅读 354
class Solution {
List<List<Integer>> res;
List<Integer> path;
int n;
int k;
public List<List<Integer>> combine(int n, int k) {
res = new ArrayList<>();
path = new ArrayList<>();
this.n = n;
this.k = k;
dfs(1);
return res;
}
private void dfs(int index) {
if (path.size() == k) {
res.add(new ArrayList<>(path));
return;
}
for (int i = index; i <= n - (k - path.size()) + 1; i++) {
path.add(i);
dfs(i + 1);
path.remove(path.size() - 1);
}
}
}