关闭

[Leetcode] 40. Combination Sum II 解题报告

标签: LeetcodeCombination Sum II解题报告ArrayBacktracking
241人阅读 评论(0) 收藏 举报
分类:

题目

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]
]

思路

1、最终去重法:思路和Leetcode 39基本一致,但是在得到结果之后,去掉结果中的重复元素,返回最终结果。这种思路耗费的时间复杂度较高,不上代码。

2、中间去重法:在backtracking的过程中保证不出现重复的解。方法是在每一层深搜的过程中,分别处理两种不相交的情况:1)不包括当前元素;2)包含当前元素,但是在递归进行下一层搜索之后,跳过所有和当前元素相同的元素,这是因为:所有包含这些相同元素的解在递归进行下一层搜索的过程中都已经找出来了。

代码

class Solution {
public:
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) 
    {
        vector<vector<int>> ret;
        if(candidates.size() == 0)
            return ret;
        sort(candidates.begin(), candidates.end());
        vector<int> line;
        combinationSum2(candidates, 0, target, ret, line);
        return ret;
    }
private:
    void combinationSum2(vector<int>& candidates, int index, int target, 
        vector<vector<int>>& ret, vector<int> &line)
    {
        if(target == 0)
        {
            ret.push_back(line);
            return;
        }
        if(index == candidates.size() || candidates[index] > target)
            return;
        // try to make candidates[index] included in the ret
        line.push_back(candidates[index]);
        combinationSum2(candidates, index + 1, target - candidates[index], ret, line);
        while(index < candidates.size() - 1 && candidates[index] == candidates[index + 1]) 
            ++index;  
        line.pop_back();
        // try to make candidates[index] excluded in the ret
        combinationSum2(candidates, index + 1, target, ret, line);
    }
};



0
0
查看评论

[leetcode]40. Combination Sum II ,python实现【medium难度】

Combination sum 2.python实现
  • zl87758539
  • zl87758539
  • 2016-06-16 17:37
  • 710

Combination Sum II -- LeetCode

原题链接: http://oj.leetcode.com/problems/combination-sum-ii/  这道题跟Combination Sum非常相似,不了解的朋友可以先看看,唯一的区别就是这个题目中单个元素用过就不可以重复使用了。乍一看好像区别比较大,但是其实实...
  • linhuanmars
  • linhuanmars
  • 2014-03-13 04:30
  • 15559

[leetcode]39. Combination Sum,python实现【Medium难度】

39.COMBINATION SUM,PYTHON实现
  • zl87758539
  • zl87758539
  • 2016-06-16 17:01
  • 1459

40. Combination Sum II(Python3)

40. Combination Sum II(Python3) 题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where th...
  • grape875499765
  • grape875499765
  • 2018-01-26 18:45
  • 33

leetcode【39+40+216+377 Combination Sum 相关】【python】

39Combination Sum是说让我们在给定数组C中找到所有的组合,使得组合中数字的和是target值。并且组合里的数字可以重复,也就是不限制C中每一个数字的使用。 这明显是回溯啦,大家记得让你判断有多少种呢就是动态规划,一旦让我们写出全部的组合那就是回溯。 回溯就是一直加进去数,满足条件...
  • u014251967
  • u014251967
  • 2016-12-13 16:38
  • 344

(Java)LeetCode-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...
  • u012848330
  • u012848330
  • 2016-08-11 23:33
  • 255

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. Eac...
  • u012501459
  • u012501459
  • 2015-07-06 21:17
  • 794

LeetCode --- 40. 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 nu...
  • makuiyu
  • makuiyu
  • 2015-02-06 21:39
  • 575

[LeetCode 39&40] Combination Sum I & II

题目链接:combination-sum import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Given a set of candidate numbers (C) ...
  • ever223
  • ever223
  • 2015-03-28 23:41
  • 826

【LeetCode】Combination Sum I & 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 n...
  • ljiabin
  • ljiabin
  • 2014-12-16 11:06
  • 6472
    个人资料
    • 访问:110221次
    • 积分:7459
    • 等级:
    • 排名:第3503名
    • 原创:644篇
    • 转载:10篇
    • 译文:2篇
    • 评论:17条
    最新评论