LeetCode40 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 to T.

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

Note:

  • All numbers (including target) will be positive integers.
  • 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]
]

分析:

主体回溯框架和Combination Sum I一致,不同之处在于一个元素不能重复用,同时不能出现重复的组合。

所以DFS那一个条件 start 变为start + 1;

同时插入result之前判定是否出现过重复的结果。

代码:

 1 class Solution {
 2 private:
 3     vector<vector<int>>result;
 4     void dfs(int start, int end, const vector<int>& candidates, int target, vector<int>& internal) {
 5         if (start > end) {
 6             return;
 7         }
 8         if (candidates[start] == target) {
 9             internal.push_back(candidates[start]);
10             if ( find(result.begin(), result.end(), internal) == result.end()) {
11                 result.push_back(internal);
12             }
13             internal.pop_back();
14             return;
15         }
16         if (candidates[start] > target) {
17             return;
18         }
19         dfs(start + 1, end, candidates, target, internal);
20         internal.push_back(candidates[start]);
21         dfs(start + 1, end, candidates, target - candidates[start], internal);
22
23         internal.pop_back();
24     }
25 public:
26     vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
27         sort(candidates.begin(), candidates.end());
28         int end = candidates.size() - 1;
29         vector<int> internal;
30         dfs(0, end, candidates, target, internal);
31         return result;
32     }
33 };
时间: 2024-08-05 06:57:01

LeetCode40 Combination Sum II的相关文章

【leetcode】Combination Sum II

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 to T. Each number in C may only be used once in the combination. Note: All numbers (including t

LeetCode: Combination Sum II 解题报告

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 to T. Each number in C may only be used once in the combination. Note:All numbers (including ta

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][JavaScript]Combination Sum II

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 to T. Each number in C may only be used once in the combination. Note: All numbers (including t

LeetCode 040 Combination Sum II

题目要求: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 to T. Each number in C may only be used once in the combination. Note: All numbers (includ

Combination Sum II leetcode java

题目: 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

040 Combination Sum II

040 Combination Sum II 这道题是 039 Combination Sum 的延伸, 稍作改动就好 class Solution: # @param {integer[]} candidates # @param {integer} target # @return {integer[][]} def combinationSum2(self, candidates, target): candidates.sort() ans = [] self.help(candidat

Combination Sum,Combination Sum II,Combination Sum III

39. 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 (inc

leetcode第一刷_Combination Sum Combination Sum II

啊啊啊啊,好怀念这种用递归保存路径然后打印出来的题目啊,好久没遇到了. 分了两种,一种是可以重复使用数组中数字的,一种是每个数字只能用一次的.其实没有多大区别,第一种每次进入递归的时候都要从头开始尝试,第二种要找一个标记的数组,把已经用到过的排除掉,就像生成全排列时的做法一样.跟我一样用引用保存中间结果的话,要注意回退的情况.第二种回退时,要把用到的那个数也恢复为可用,就完全像全排列时做的一样.破例贴两个题的代码,因为他们是在是不值得用两片文章来写. class Solution { publi