题目:
解析:
代码:
private List<List<Integer>> ret; private List<Integer> path; private int n,k; public List<List<Integer>> combine(int _n, int _k) { n = _n; k = _k; path = new ArrayList<>(); ret = new ArrayList<>(); dfs(1); return ret; } private void dfs(int start){ if(path.size() == k){ ret.add(new ArrayList(path)); return; } for(int i = start; i <= n; i++){ path.add(i); dfs(i+1);//剪枝:选过接着往后选 path.remove(path.size()-1);//恢复现场 } }