[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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 纯净的系统重装工具(使用小白一键重装系统之后的系统)

    重装系统教程入门IT第一步,来一个干净的电脑系统吧?(装机需谨慎,系统盘必须格式化,其他风险自测。)本教程提供两种重装系统方式,两种均为纯净装机,均需要U盘。1、PE重装,此方式会为装机者提供更多工具,包括分区大师,电脑密码破解等等工具。2、ISO重装,此方法傻瓜式操作,简单快捷,一键重装。PE重装工具:1、8G以上U盘2、微PE工具:连接:https://pan.baidu…

    2022年4月14日
    120
  • 使用blackice 服务器版默认设置里需要曾加的几个信任

    使用blackice 服务器版默认设置里需要曾加的几个信任   今天在天天安全网下载了blackice的3.6coa版,先是在官方看到了最高版本,然后想到更新自己的软件,结果在网上找到了这个。安装更新后果然里面增强了很多事件拦截,而过滤也比原来强了。常常弄得自己服务器都不能连接,开始总以为服务器当了,可后来换了IP就能上了。才发现原来是blackice所把我的IP自动拦截了。  服务器上装了helix、http,别人在看电影时都会被拦了,我自己在网上更

    2022年5月25日
    43
  • ssm共享充电宝管理系统计算机毕业设计[通俗易懂]

    ssm共享充电宝管理系统计算机毕业设计[通俗易懂]最新200套计算机专业原创毕业设计参考选题都有源码+数据库是近期作品如果题目不合适,可以去我上传的资源里面找题目,找不到的话,评论留下题目,站内私我或add用户名,有时间看到机会给您发1 3865ssm共享充电宝管理系统 2 583拼餐网站2018 3 3592ssm基于SSM健身房管理系统 4 3391springboot基地信息可视化 5 3202springcloud基于springcloud的电商平台的设计与实现 6 4686spring

    2022年6月4日
    39
  • idea设置删除一行快捷键_删除空白页快捷键

    idea设置删除一行快捷键_删除空白页快捷键IDEA删除当前一行快捷键默认是Ctrl+Y但是有时候会和反撤销键冲突,所以还是自己设置一下也可以自定义双击选中输入框,在键盘上按下ctrl+y就可以了。然后点击OK就可以了当然你也可以直接使用Ctrl+X剪切的快捷键,也是可以充当删除当前行的快捷键…

    2025年9月29日
    2
  • visio2013密钥激活_visio2013不激活可以用吗

    visio2013密钥激活_visio2013不激活可以用吗  2NYF6-QG2CY-9F8XC-GWMBW-29VV8  FJ2N7-W8TXC-JB8KB-DCQ7Q-7T7V3  VXX6C-DN3HQ-3CRXG-RF4KT-YG7V3  B3C7Q-D6NH2-2VRFW-HHWDG-FVQB6  TCWJK-N6GFH-82BP9-HV7YQ-T6KMQ  KD8CP-DN968-RGQM6-PMFMK-M96YQ  …

    2025年8月28日
    5
  • RTMP协议

    目录简介概念rtmp协议握手过程rtmp通信过程简介RTMP协议是RealTimeMessageProtocol(实时信息传输协议)的缩写,它是由Adobe公司提出的一种应用层的协议,用来解决多媒体数据传输流的多路复用(Multiplexing)和分包(packetizing)的问题。     RTMP消息块流和RTMP一起适用于多样性音视频应用程序,从…

    2022年4月3日
    193

发表回复

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

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