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


相关推荐

  • django urls_关于URL的作用

    django urls_关于URL的作用前言为什么我们url需要命名呢?url命名的作用是什么?我们先来看一个案例案例我们先在一个Django项目中,创建2个App,前台front和后台cms,然后在各自app下创建urls.py文件

    2022年7月28日
    5
  • 恢复pycharm中误删的Python文件

    恢复pycharm中误删的Python文件(1)找到删除的上一级文件=>右键,找到LocalHistory=>ShowHistory(2),打开之后,双击左边的恢复时间内容,在右边操作框中,恢复,这里只能一个一个恢复(3)注意,这里恢复,应该选择第二个”RevertSelection”.(4)再恢复的时候可能需要等几秒,这样就成功恢复了….

    2022年8月25日
    10
  • 程序员的财务自由之路(一)- 扬帆起航

    程序员的财务自由之路(一)- 扬帆起航程序员的财务自由之路

    2022年7月25日
    9
  • matlab画圆的命令_matlab画圆命令资料

    matlab画圆的命令_matlab画圆命令资料%%圆环面R=6;r=2;symsuv;ezmesh((R+r*cos(u))*cos(v),(R+r*cos(u))*sin(v),r*sin(u));axisequal;%%圆盘R=6;r=2;theta=linspace(0,2*pi,90);ph=linspace(r,R,30);[t,p]=meshgrid(theta,ph);r=t*0;[x,y,z]=pol…

    2022年6月19日
    30
  • linux系统如何修改用户名_两种方案对比报告

    linux系统如何修改用户名_两种方案对比报告在安装系统的时候,经常会无意识的随便起个用户名,后面如果发现该用户名不好或因为环境需要须重起个用户名,经过查找资料和亲自测试发现有两种方案可选:手动修改和使用usermod等命令自动修改。方案一:手动修改各个关联用户文件以root身份登录系统文件的修改需要root权限,这里不管是原root用户登录还是普通用户切换到root下都可以,只要有修改权限就行。修改/etc/passwd这个文件中的用户名部

    2022年9月13日
    2
  • 数据结构单链表的算法描述_数据结构创建单链表及其实现

    数据结构单链表的算法描述_数据结构创建单链表及其实现一、什么是链表链表是一种数据结构,跟数组不同,链表不需要连续的内存空间,而是通过指针将零散的内存块连接起来。因此,链表的查找需要通过节点按顺序遍历,而增加与删除通过只需要操作指针指向,这也造成了相

    2022年8月16日
    11

发表回复

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

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