[leetcode] Combination Sum and Combination SumII

[leetcode] Combination Sum and Combination SumII

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

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 be positive integers.
  • Elements in a combination (a1a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
  • The solution set must not contain duplicate combinations.

For example, given candidate set 2,3,6,7 and target 7
A solution set is: 
[7] 
[2, 2, 3] 

class Solution {
private:
    vector<vector<int> > ivvec;
public:
    vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
        vector<int> ivec;
        sort(candidates.begin(), candidates.end());
        combinationSum(candidates, 0, target, ivec);
        return ivvec;
    }
    
    void combinationSum(vector<int> &candidates, int beg, int target, vector<int> ivec)
    { 
        if (0 == target)
        {
            ivvec.push_back(ivec);
            return;
        } 
        for (int idx = beg; idx < candidates.size(); ++idx)
        {
            if (target - candidates[idx] < 0)
                break;
            ivec.push_back(candidates[idx]);
            combinationSum(candidates, idx, target - candidates[idx], ivec);
            ivec.pop_back();
        }
    }
};

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.
  • Elements in a combination (a1a2, … , 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] 

与上题差别不大。依然是用DFS,基本的问题在于怎样去重。

能够添加一个剪枝: 当当前元素跟前一个元素是同样的时候。假设前一个元素被取了,那当前元素能够被取,也能够不取,反过来假设前一个元素没有取。那我们这个以及之后的所以同样元素都不能被取。

(採用flag的向量作为标记元素是否被选取)

class Solution {private:    vector<vector<int> > ivvec;    vector<bool> flag;public:    vector<vector<int> > combinationSum2(vector<int> &num, int target) {        vector<int> ivec;        sort(num.begin(), num.end());        flag.resize(num.size());        for (int i = 0; i < flag.size(); ++i)            flag[i] = false;        combinationSum2(num, 0, ivec, target, 0);        return ivvec;    }        void combinationSum2(vector<int> &num, int beg, vector<int> ivec, int target, int sum)    {        if (sum > target)            return;        if (sum == target)        {            ivvec.push_back(ivec);            return;        }                for (int idx = beg; idx < num.size(); ++idx)        {            if (sum + num[idx] > target) continue;            if (idx != 0 && num[idx] == num[idx - 1] && flag[idx - 1] == false)                continue;            flag[idx] = true;            ivec.push_back(num[idx]);            combinationSum2(num, idx + 1, ivec, target, sum + num[idx]);            ivec.pop_back();            flag[idx] = false;        }    }};

版权声明:本文博主原创文章。博客,未经同意不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117022.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • matlab二元函数求极值例题_matlab求二元函数最大值

    matlab二元函数求极值例题_matlab求二元函数最大值MATLAB一元函数与二元函数求极小值

    2022年9月3日
    5
  • 公有云和私有云哪种成本更低

    公有云和私有云哪种成本更低​最新调查表明:在虚拟机运行数量相同的情况下,企业使用私有云比公有云更加节省成本。随着企业对私有云和公有云、云服务器的使用经验加深,很多企业表示已经通过实践把握私有云的部署和正确使用,并在成本方面超越公有云。据著名互联网调查研究公司对150位IT决策者调查显示,41%的受访者表示每个虚拟机运行在私有云基础架构比公共云成本更低。在某些情况下,节省的费用非常惊人。9%的受访者表示,他们在私有云运行…

    2022年6月16日
    26
  • dede:list及dede:arclist 按权重排序的方法

    dede:list及dede:arclist 按权重排序的方法

    2021年9月22日
    38
  • drupal 6.0 入门教程 – 第一章

    drupal 6.0 入门教程 – 第一章
     
    由于工作项目的原因,需要采用drupal来部署,所以最近学习了drupalcms,天天到 drupal.org,drupalchina.org,zhupou.cn,5iphp.com上学习
     
     
    项目的核心是提供一款在线教学和互动社区,希望通过这个教程提供给大家一个比较全面的项目开发指导。首先,我近期的主要任务是熟悉drupalCMS,和设计主页的版式也就是themes。
     
    下面我们从drupal的介绍入手,开始讲解如果

    2022年5月29日
    34
  • ValidateRequest问题

    ValidateRequest问题1,在出现该错误的页面头部的page中加入ValidateRequest="false",那么该页面的任何一次Post提交都不会再验证提交内容的安全性。如:<%@&#160

    2022年7月2日
    20
  • 外推人员如何寻找好的外推平台?浅谈发外推网寻找平台的一些技巧

    外推人员如何寻找好的外推平台?浅谈发外推网寻找平台的一些技巧很多从事外推职业的朋友总是在为找不到好的平台而郁闷,发外推网的QQ群里也总是有人在询问最近有什么排名比较好的平台,求发布平台;其实找可以发布的外推平台并不是一件多么麻烦的事情,下面就来简单介绍一下常见

    2022年7月1日
    26

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号