首页 > 代码库 > 40. Combination Sum II
40. 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] ]来源: https://leetcode.com/problems/combination-sum-ii/
分析
这里和39类似,都是使用DFS。
唯一需要注意的是:[1,1,2,5,6,7,10] 中,找到8
如果使用39类似的方法,会出现两个[1,7]。 因为有两个重复的1。
解决方法:
在每一次调用helper函数的时候(代表同一层遍历)如果遇到c[i] == c[i - 1],比如已经把 index = 0 的1所有的可能DFS过了,那么再遇到 index = 1 的 1时候,就跳过,不进行DFS,因为之前的结果一定包含这次的结果。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | class Solution { public : vector<vector< int >> combinationSum2(vector< int >& candidates, int target) { vector<vector< int >> results; sort(candidates.begin(), candidates.end()); helper(results, vector< int >{}, candidates, target, 0); return results; } void helper(vector<vector< int >> &results, vector< int > result, vector< int >& c, int target, int index) { for ( int i = index; i < c.size(); ++i) { int t = target - c[i]; if (t < 0) { return ; } else if (i==index || c[i] != c[i - 1]){ // jump over duplicate results result.push_back(c[i]); if (t == 0) { results.push_back(result); } else { helper(results, result, c, t, i + 1); // i + 1: insure every element is used once } result.pop_back(); } } } }; |
精简版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | class Solution { public : vector<vector< int >> combinationSum2(vector< int >& candidates, int target) { vector<vector< int >> results; sort(candidates.begin(), candidates.end()); helper(results, vector< int >{}, candidates, target, 0); return results; } void helper(vector<vector< int >> &results, vector< int > result, vector< int >& c, int target, int index) { if ( target == 0){ results.push_back(result); return ; } for ( int i = index; i < c.size() && target >= c[i]; ++i) { if (i==index || c[i] != c[i - 1]){ // jump over duplicate results result.push_back(c[i]); helper(results, result, c, target - c[i], i + 1); // i + 1: insure every element is used once result.pop_back(); } } } }; |
null
40. Combination Sum II
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。