雄安智能网站建设方案,做涉黄的视频网站用什么服务器,工作服定制,郑州网络营销网站优化题目
给定一个候选人编号的集合 candidates 和一个目标数 target #xff0c;找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的每个数字在每个组合中只能使用 一次 。
注意#xff1a;解集不能包含重复的组合。
示例 1:
输入: candidates [10,1,…题目
给定一个候选人编号的集合 candidates 和一个目标数 target 找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的每个数字在每个组合中只能使用 一次 。
注意解集不能包含重复的组合。
示例 1:
输入: candidates [10,1,2,7,6,1,5], target 8, 输出: [ [1,1,6], [1,2,5], [1,7], [2,6] ]
解
class Solution {public ListListInteger combinationSum2(int[] candidates, int target) {ListListInteger result new ArrayList();LinkedListInteger path new LinkedList();Arrays.sort(candidates);dfs(candidates, 0, target, path, result);return result;}public void dfs(int[] candidates, int index, int target, LinkedListInteger path, ListListInteger result) {if (target 0) {result.add(new LinkedList(path));return;}if (target 0) {return;}for (int i index; i candidates.length; i) {if (i index candidates[i] candidates[i - 1]) {continue;}path.add(candidates[i]);dfs(candidates, i 1, target - candidates[i], path, result);path.removeLast();}}
}