2025-05-17提交
This commit is contained in:
36
77.combinations.java
Normal file
36
77.combinations.java
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* @lc app=leetcode id=77 lang=java
|
||||
*
|
||||
* [77] Combinations
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public List<List<Integer>> combine(int n, int k) {
|
||||
List<List<Integer>> r = new ArrayList<List<Integer>>();
|
||||
List<Integer> rt = new ArrayList<Integer>();
|
||||
dfsIterate(r,rt, 1, n, k);
|
||||
return r;
|
||||
}
|
||||
|
||||
private void dfsIterate(List<List<Integer>> r, List<Integer> list, int c, int n, int k) {
|
||||
if(list.size() == k) {
|
||||
r.add(shallowCopy(list));
|
||||
return;
|
||||
}
|
||||
if(c > n) {
|
||||
return;
|
||||
}
|
||||
for(int i = c; i<=n; i++) {
|
||||
List<Integer> p = shallowCopy(list);
|
||||
p.add(i);
|
||||
dfsIterate(r, p, i+1, n, k);
|
||||
}
|
||||
}
|
||||
|
||||
private List<Integer> shallowCopy(List<Integer> list) {
|
||||
return new ArrayList<>(list);
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
Reference in New Issue
Block a user