2025-05-17提交

This commit is contained in:
asahi
2025-05-17 17:01:56 +08:00
parent c4e1e74677
commit a62543b3fd
7 changed files with 454 additions and 0 deletions

36
77.combinations.java Normal file
View 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