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