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. SqlServer操作远程数据库

    exec sp_addlinkedserver 'srv2','','mssql2008','服务器IP' exec sp_addlinkedsrvlogin 'srv2','false',null, ...

  2. Java中定时器的使用

    import java.text.SimpleDateFormat; import java.util.Date; import java.util.Timer; import java.util.T ...

  3. [POJ 3150] Cellular Automaton (矩阵高速幂 + 矩阵乘法优化)

    Cellular Automaton Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 3048   Accepted: 12 ...

  4. iOS很重要的 block回调

    刚刚进入ios开发行业,发现开发中要用到大量的block回调,由此可见它的重要性.学习它之前我也是网上找的资料,推荐这篇文章http://blog.csdn.net/mobanchengshuang/ ...

  5. margin 还能够被缩回

    <p><strong>话:</strong>的肥沃和收获而被估价的.才干也是土地,只是它生产的不是粮食,而是真理.假设仅仅能滋生瞑想和幻想的话,即使再大的才干也仅仅 ...

  6. ASP.NET Core环境并运行 继续跨平台

    ASP.NET Core环境并运行 继续跨平台 无需安装mono,在Linux(Ubuntu)下搭建ASP.NET Core环境 继续.NET跨平台 上一篇:使用VS Code开发ASP.NET Co ...

  7. Windows Phone开发(33):路径之其它Geometry

    原文:Windows Phone开发(33):路径之其它Geometry 上一节中,我们把最复杂的PathGeometry给干了,生剩下几个家伙就好办事了.一起来见见他们的真面目吧. 一.LineGe ...

  8. Leet code —Jump Game

    问题叙述性说明: Given an array of non-negative integers, you are initially positioned at the first index of ...

  9. meta标签详解:源http://blog.csdn.net/kongjiea/article/details/17092413

    一.大众机型常用meta标签name的设置 1.name之viewport <meta name="viewport" content=""> 说明 ...

  10. WPF 各种基础动画实现

    C# using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syste ...