[leetcode]Combination SumII

Combination Sum II

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums toT.

Each number in C may only be used once in the combination.

Note:

  • All numbers (including target) will be positive integers.
  • Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
  • The solution set must not contain duplicate combinations.

For example, given candidate set 10,1,2,7,6,1,5 and target 8
A solution set is: 
[1, 7] 
[1, 2, 5] 
[2, 6] 
[1, 1, 6]

算法思路:

[leetcode]Combination Sum类似,区别是这里的元素要求不能重复,且结果需要去重。

代码如下:

 1 public class Solution {
 2      List<List<Integer>> result = new ArrayList<List<Integer>>();
 3     public List<List<Integer>> combinationSum2(int[] num, int target) {
 4         if(num == null || num.length == 0) return result;
 5         Arrays.sort(num);
 6         List<Integer> list = new ArrayList<Integer>();
 7         dfs(num,list,0,target);
 8         return result;
 9     }
10
11     private void dfs(int[] num,List<Integer> list,int k,int target){
12         if(target == 0){
13             List<Integer> copy = new ArrayList<Integer>(list);
14             for(List<Integer> node : result){
15                 int count = 0;
16                 if(copy.size() == node.size()){
17                     for(int i = 0; i < node.size(); i++){
18                         if(copy.get(i) == node.get(i)){
19                             count++;
20                         }else break;
21                     }
22                     if(count == node.size()) return;
23                 }
24             }
25             result.add(copy);
26             return;
27         }
28         if(k >= num.length || num[k] > target) return;
29         for(int i = k; i < num.length; i++){
30             list.add(num[i]);
31             dfs(num,list,i + 1, target - num[i]);
32             list.remove(list.size() - 1);
33         }
34     }
35 }

去重这里我用了最土的算法,对每一组新插入的数组,检查result中是否存在相同的组合,这无疑会做很多无用功,而且每次遍历的时间也很浪费,后来偷懒用了之前的去重方法,在每一次进行dfs之前先检查是否与前驱相同。

 1     private void dfs(int[] num,List<Integer> list,int k,int target){
 2         if(target == 0){
 3             List<Integer> copy = new ArrayList<Integer>(list);
 4             result.add(copy);
 5             return;
 6         }
 7         if(k >= num.length || num[k] > target) return;
 8         for(int i = k; i < num.length; i++){
 9             if(i > k && num[i] == num[i - 1]) continue;//这里本来写的是i>0,结果毫无疑问的跪了。
10             list.add(num[i]);
11             dfs(num,list,i + 1, target - num[i]);
12             list.remove(list.size() - 1);
13         }
14     }

后来看了jd童鞋的算法,对代码进行了不一样的优化:

完整代码如下:

 1 public class Solution {
 2      List<List<Integer>> result = new ArrayList<List<Integer>>();
 3     public List<List<Integer>> combinationSum2(int[] num, int target) {
 4         if(num == null || num.length == 0) return result;
 5         Arrays.sort(num);
 6         List<Integer> list = new ArrayList<Integer>();
 7         dfs(num,list,0,target);
 8         return result;
 9     }
10
11     private void dfs(int[] num,List<Integer> list,int k,int target){
12         if(target == 0){
13             List<Integer> copy = new ArrayList<Integer>(list);
14             result.add(copy);
15             return;
16         }
17         if(k >= num.length || num[k] > target) return;
18         for(int i = k; i < num.length; i++){
19             list.add(num[i]);
20             dfs(num,list,i + 1, target - num[i]);
21             list.remove(list.size() - 1);
22             while (i < num.length - 1 && num[i] == num[i + 1]) i++;
23         }
24     }
25 }
while (i < num.length - 1 && num[i] == num[i + 1]) i++;用的很漂亮,总而言之,对DFS还是不能运用自如。无论是哪种方法,优化完之后,代码的执行次数毫无疑问会减少很多,更主要的是省去了不必要的每次的查重,虽然时间复杂度是一样的,但是从代码可读性和实际执行时间来看,无疑后者更优

[leetcode]Combination SumII

时间: 2024-10-07 16:42:34

[leetcode]Combination SumII的相关文章

[leetcode]Combination Sum @ Python

原题地址:https://oj.leetcode.com/problems/combination-sum/ 题意: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited

LeetCode: Combination Sum [038]

[题目] Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target)

LeetCode: Combination Sum II [039]

[题目] Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be

Leetcode:Combination Sum 子集和问题

Combination Sum: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (includ

Leetcode | Combination Sum I &amp;&amp; II

Combination Sum I Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note:All numbers (includ

[leetcode] Combination Sum and Combination SumII

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target) will

Leetcode dfs Combination SumII

Combination Sum II Total Accepted: 13710 Total Submissions: 55908My Submissions Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be

LeetCode Combination Sum III

原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 与Combination Sum II相似,不同的是中不是所有元素相加,只是k个元素相加. 所以在把item的copy 加到res前需要同时满足item.size() == k 和 target == 0两个条件. AC Java: 1 public class Solution { 2 public List<List<Integer>> combinationS

LeetCode: Combination Sum 解题报告

Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Question Solution Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The