【DFS】LeetCode 77. 组合
题目链接
思路
DFS
模板题
代码
class Solution {
private List<List<Integer>> result = new ArrayList<>();
private Deque<Integer> path = new ArrayDeque<>();
public List<List<Integer>> combine(int n, int k) {
dfs(1, n, k);
return result;
}
void dfs(int current, int n, int k){
if(path.size() == k){
result.add(new ArrayList<>(path));
return;
}
for(int i = current; i <= n; i++){
path.addLast(i);
dfs(i + 1, n, k);
path.removeLast();
}
}
}