1 Star 0 Fork 0

Neo_1001/LeetCode刷题记录

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
LC_77.java 901 Bytes
一键复制 编辑 原始数据 按行查看 历史
Neo_1001 提交于 2024-04-10 22:16 . first commit
package LeetCode;
import java.util.ArrayList;
import java.util.List;
public class LC_77 {
static ArrayList<Integer> path = new ArrayList<>();
static List<List<Integer>> res = new ArrayList<>();
public static void main(String[] args) {
combine(5, 4);
System.out.println(res);
}
public static List<List<Integer>> combine(int n, int k) {
backtracing(n, k, 1);
return res;
}
public static void backtracing(int n, int k, int start) {
if (path.size() == k) {
//res.add(path); //这样是将path的引用添加到了res中,是不对的,后续对path的修改会影响res的值
res.add(new ArrayList<>(path));
return;
}
for (int i = start; i <= n; i++) {
path.add(i);
backtracing(n, k, i + 1);
path.remove(path.size() - 1);
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/neo1001/leet-code-practice-record.git
git@gitee.com:neo1001/leet-code-practice-record.git
neo1001
leet-code-practice-record
LeetCode刷题记录
master

搜索帮助