1 Star 0 Fork 0

Neo_1001/LeetCode刷题记录

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
LC_40.java 1.30 KB
一键复制 编辑 原始数据 按行查看 历史
Neo_1001 提交于 2024-04-10 22:16 . first commit
package LeetCode;
import java.util.*;
public class LC_40 {
static List<List<Integer>> res = new ArrayList<>();
static ArrayList<Integer> path = new ArrayList<>();
public static void main(String[] args) {
int[] candidates = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
int target = 27;
combinationSum2(candidates, target);
}
public static List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> resList = new ArrayList<>();
if (candidates == null || candidates.length == 0) return resList;
Arrays.sort(candidates);
backtracking(candidates, target, 0);
System.out.println(res);
return res;
}
public static void backtracking(int[] candidates, int target, int startIndex) {
if (path.stream().mapToInt(Integer::intValue).sum() == target) {
res.add(new ArrayList<>(path));
return;
}
if (path.stream().mapToInt(Integer::intValue).sum() > target) return;
for (int i = startIndex; i < candidates.length; i++) {
if (i > startIndex && candidates[i] == candidates[i - 1]) continue;
path.add(candidates[i]);
backtracking(candidates, target, 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

搜索帮助