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


相关推荐

  • Uefi安装Centos7出现错误以及解决方法

    Uefi安装Centos7出现错误以及解决方法写这篇就当是学习的笔记和总结。文笔不好有什么错别字或不通的地方大家多担待。很少使用Linux系统,前段时间因工作需要,要在一台服务器上安装centos7,服务器默认的引导方式是Uefi,下载ISO镜像用UltraISO刻U盘后引导安装但是报错,进后dracut#命令行,当时完全是懵的一堆英文单字没几个认识。只能百度搜索出错原因和解决方法,以下就是网上说的方法和自己实践的总结。…

    2022年6月25日
    34
  • graduation和completion_guides和maven区别

    graduation和completion_guides和maven区别参考:androidgradle依赖:implementation和compile的区别2017年google后,Androidstudio版本更新至3.0,更新中,连带着com.android.tools.build:gradle工具也升级到了3.0.0,在3.0.0中使用了最新的Gralde4.0里程碑版本作为gradle的编译版本,该版本gradle编译速度有所加速,更加……

    2025年7月24日
    3
  • 群体智能优化算法之鲸鱼优化算法(Whale Optimization Algorithm,WOA)

    群体智能优化算法之鲸鱼优化算法(Whale Optimization Algorithm,WOA)获取更多资讯,赶快关注上面的公众号吧!文章目录鲸鱼优化算法(WhaleOptimizationAlgorithm,WOA)1.1灵感1.2数学建模和优化算法1.2.1包围捕食(Encirclingprey)1.2.2气泡网攻击方式(Bubble-netattackingmethod)(利用阶段)1.2.3搜索猎物(Searchforprey)(explorationph…

    2022年5月23日
    53
  • fseek函数用法_fwrite函数的用法

    fseek函数用法_fwrite函数的用法转载请注明出处:https://blog.csdn.net/wl_soft50/article/details/7787521每天进步一点点–>函数fseek()用法在阅读代码时,遇到了很早之前用过的fseek(),很久没有用了,有点陌生,写出来以便下次查阅。函数功能是把文件指针指向文件的开头,需要包含头文件stdio.hfseek函数名:fseek功能:重定位流上的文件…

    2025年8月25日
    3
  • 119149_1125*2436

    119149_1125*2436题意理解:http://acm.timus.ru/problem.aspx?space=1&amp;num=1142有N个对象,问有多少种关系?问题分析:用动态规划做:f(a,b)表示a个对象分成b组的分法。b组的意思是a个对象放到b个篮子里,每个篮子的对象之间是相等关系。初始值:f(0,0)=1;f(0,1…N)=0;f(1…N,0)=0递归式:f(a,b)=f(…

    2022年9月28日
    2
  • Ubuntu中Anaconda安装opencv3[通俗易懂]

    Ubuntu中Anaconda安装opencv3[通俗易懂]关于如何安装,这篇blog中已经给出了很好的方法:https://blog.csdn.net/isuccess88/article/details/73546835,但由于自前段时间开始换源已经不能解决anaconda的下载速度,因此即使使用此方法也很难进行下去,下载速度太慢了。我特地下载了opencv3的opencv3-3.2.0-py35(链接:https://pan.baidu.com…

    2022年10月19日
    1

发表回复

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

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