代码拉取完成,页面将自动刷新
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);
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。