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 (a1, a2,
    … , 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 (a1, a2,
    … , 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;
}
}
};

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

[leetcode] Combination Sum and Combination SumII的更多相关文章

  1. Combination Sum 和Combination Sum II

    这两道题的基本思路和combination那一题是一致的,也是分治的方法. 其中combination Sum复杂一点,因为每个数可能用多次.仔细分析下,本质上也是一样的.原来是每个数仅两种可能.现在 ...

  2. Combination Sum,Combination Sum II,Combination Sum III

    39. Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique co ...

  3. 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV

    ▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...

  4. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  5. [LeetCode] Combination Sum III 组合之和之三

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  6. [LeetCode] Combination Sum II 组合之和之二

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  7. [LeetCode] Combination Sum 组合之和

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  8. [LeetCode] 377. Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  9. [LeetCode] 40. Combination Sum II 组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

随机推荐

  1. C++:抽象基类和纯虚函数的理解

    转载地址:http://blog.csdn.net/acs713/article/details/7352440 抽象类是一种特殊的类,它是为了抽象和设计的目的为建立的,它处于继承层次结构的较上层. ...

  2. IIS总提示输入用户名和密码

    解决办法: 第一步,在"运行"中输入"gpedit.msc"调出组策略设置. 找到计算机配置-管理模块-windows 组件-internet控制面板-安全页- ...

  3. 成为JAVA软件开发工程师要学哪些东西

    2010-04-22 15:34 提问者采纳 Java EE(旧称j2ee)   第一阶段:Java基础,包括java语法,面向对象特征,常见API,集合框架: *第二阶段:java界面编程,包括AW ...

  4. 怎样让你的安卓手机瞬间变Firefox os 畅玩firefox os 应用

    Firefox os 手机迟迟不能在国内大面积上市.如今能买到的Firefox os手机国内就一款Firefox os ZET OPEN C ,但这款手机配置确实还不如人意.价格方面也不实惠,对于我们 ...

  5. mini2440裸试验—计算器(LCD显示,触摸屏突破)

    关于Pait_Bmp(x0, y0, x, y, BMPaddr);函数 像素图在屏幕左上角为(0,0),Pait_Bmp中的x0.y0分别像素点初始位置,x,y为BMP图片的X,Y的大小.BMPad ...

  6. hdu5618 (三维偏序,cdq分治)

    给定空间中的n个点,问每个点有多少个点小于等于自己. 先来分析简单的二维的情况,那么只要将x坐标排序,那么这样的问题就可以划分为两个子问题,,这样的分治有一个特点,即前一个子问题的解决是独立的,而后一 ...

  7. hdu1506(dp减少重复计算)

    可以算出以第i个值为高度的矩形可以向左延伸left[i],向右延伸right[i]的长度 那么答案便是 (left[i] + right[i] + 1) * a[i] 的最大值 关键left[i] 和 ...

  8. codechef Little Elephant and Permutations题解

    The Little Elephant likes permutations. This time he has a permutation A[1], A[2], ..., A[N] of numb ...

  9. linux设备驱动程序第四部分:从如何定位oops对代码的调试方法,驱动线

    在一个我们谈到了如何编写一个简单的字符设备驱动程序,我们不是神,编写肯定会失败的代码,在这个过程中,我们需要继续写代码调试.在普通c应用.我们经常使用printf输出信息.或者使用gdb要调试程序,然 ...

  10. android EditText的美化

    今天要做一个页面,有EditText,于是就搞起了它的美化. EditText的美化,我的第一反应是,在EditText的左边设置显示一张图片,这样会比較好看. 设置左边显示图片的属性为:androi ...