[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)
上一篇 2022年1月11日 下午9:00
下一篇 2022年1月11日 下午10:00


相关推荐

  • java 取当前时间年月日_Java获取当前时间年月日的方法[通俗易懂]

    java 取当前时间年月日_Java获取当前时间年月日的方法[通俗易懂]本文实例为大家分享了java获取当前时间年月日的具体代码,供大家参考,具体内容如下importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Calendar;importjava.util.Date;publicclassDateTest{publicstaticvoidmai…

    2025年9月7日
    7
  • python2 nonlocal_Python nonlocal

    python2 nonlocal_Python nonlocalpython3:变量作用域及global,nonlocal的用法在Python程序中声明、改变、查找变量名时,都是在一个保存变量名的命名空间中进行中,此命名空间亦称为变量的作用域。python的作用域是静态的,在代码中变量名被赋值的位置决定了该变量能被访问的范围。即Python变量的作用域由变量所在源代码中的位置决定.变量作用域之LENGBL=Local局部作用域E=…

    2025年9月20日
    15
  • python爬虫库_python爬虫实战百度云盘

    python爬虫库_python爬虫实战百度云盘如何使用爬虫与JieBa库制作词云所需库的安装所需第三方库为如下:importrequestsfrombs4importBeautifulSoupfromwordcloudimportWordCloudimportmatplotlib.pyplotaspltimportjiebaimportnumpyasnpfromPILimportImage此网址内含大量python第三方库下载安装即可:链接:https://www.lfd.uci.edu/~g

    2025年11月18日
    6
  • 用jquery制作课工场论坛发帖_jquery跨域post请求

    用jquery制作课工场论坛发帖_jquery跨域post请求  承蒙厚爱,经过培训团队的讨论之后,决定将由DflyingChen和我负责AJAX部分的培训。初期培训的方式是录制视频讲座并提供下载,等条件成熟后会逐渐转向在线培训的方式。  培训的内容为AJAX,将着重讲述AJAX在ASP.NET中的应用。初期的侧重点将是AJAX基础部分的讲解,并逐渐过渡到微软公司的ASP.NETAJAX框架。虽然会由我们来决定讲座的内容与提纲,但是由于我们的目的是服…

    2025年9月16日
    10
  • 拉格朗日插值公式详解[通俗易懂]

    拉格朗日插值公式详解[通俗易懂]一.线性插值(一次插值)   已知函数f(x)在区间[xk ,xk+1 ]的端点上的函数值yk =f(xk ),yk+1 =f(xk+1 ),求一个一次函数y=P1 (x)使得yk =f(xk ),yk+1 =f(xk+1 ),其几何意义是已知平面上两点(xk ,yk ),(xk+1 ,yk+1 ),求一条直线过该已知两点。   1.插值函数和插值基函数由直线的

    2025年8月23日
    4
  • 线性代数 — 矩阵求逆的4种方法

    线性代数 — 矩阵求逆的4种方法矩阵求逆的 4 种方法

    2025年8月15日
    5

发表回复

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

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