题目:

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]

代码:

class Solution {
public:
vector<vector<int> > combinationSum2(vector<int>& candidates, int target)
{
vector<vector<int> > ret;
std::sort(candidates.begin(), candidates.end());
int sum = ;
vector<int> tmp;
Solution::dfs(ret, tmp, sum, candidates, , candidates.size()-, target);
return ret;
}
static void dfs(
vector<vector<int> >& ret,
vector<int>& tmp,
int &sum,
vector<int>& candidates,
int begin,
int end,
int target
)
{
if ( sum>target ) return;
if ( sum==target )
{
ret.push_back(tmp);
return;
}
int pre = candidates[]-;
for ( int i=begin; i<=end; ++i )
{
if ( pre==candidates[i] ) continue;
pre = candidates[i];
if ( sum+candidates[i]<=target )
{
sum += candidates[i];
tmp.push_back(candidates[i]);
Solution::dfs(ret, tmp, sum, candidates, i+, end, target);
tmp.pop_back();
sum -= candidates[i];
}
}
}
};

tips:

此题与combination sum不同之处在于,每个元素只能取一次,并且解集中不能有重复的。

用深搜模板:

1. 如果元素都没有重复的,就是最直接的深搜模板(注意dfs到下一层的时候,传入的begin是i+1而不是i了)

2. 如果元素有重复的,再处理时就跳过前面出现过的元素(前提是candidates都排好序);这里有个技巧就是维护一个pre变量,并且初始化pre为candidates[0]-1,即比candidates元素都小,这样不用改变循环的结构就可以直接处理

3. 还有一个疑问,为什么不用判断begin>end的情况?比如,{1,1,1} ,4 这种输入,显然所有元素加一起也满足不了结果。进行到最后一定会出现begin==3 end==2的情况。这种情况也不要紧,因为begin大于end就不处理了,直接返回了,所以也没事。

===========================================

第二次过这道题,遇到这种不要重复结果的,有些规律:就是在每一层dfs的时候,如果candidate[i]出现连续两个重复,就跳过后面的那个。

class Solution {
public:
vector<vector<int> > combinationSum2(
vector<int>& candidates, int target)
{
sort(candidates.begin(), candidates.end());
vector<vector<int> > ret;
vector<int> tmp;
Solution::dfs(ret, tmp, candidates, , candidates.size()-, target);
return ret;
}
static void dfs(
vector<vector<int> >& ret,
vector<int>& tmp,
vector<int>& candidates,
int begin,
int end,
int target
)
{
if ( target< ) return;
if ( target== )
{
ret.push_back(tmp);
return;
}
if ( begin>end ) return;
int pre = candidates[begin]-;
for ( int i=begin; i<=end; ++i )
{ if ( pre==candidates[i]) continue;
pre = candidates[i];
tmp.push_back(candidates[i]);
Solution::dfs(ret, tmp, candidates, i+, end, target-candidates[i]);
tmp.pop_back();
}
}
};

【Combination Sum II 】cpp的更多相关文章

  1. 【Path Sum II】cpp

    题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...

  2. 【二叉树的递归】04找出二叉树中路径和等于给定值的所有路径【Path Sum II】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树和一个和,判断这个树 ...

  3. 【Word Break II】cpp

    题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...

  4. 【Unique Paths II】cpp

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  5. 【Spiral Matrix II】cpp

    题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...

  6. 【palindrome partitioning II】cpp

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  7. 【Jump Game II 】cpp

    题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...

  8. 【Word Ladder II】cpp

    题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...

  9. 【Single Num II】cpp

    题目: Given an array of integers, every element appears three times except for one. Find that single o ...

随机推荐

  1. Mybatis介绍(一)

    这里介绍的mybatis比较简单, 我做为一个初学者, 记录下个人在学习中方法, 如果那里出错, 希望读者朋友们见谅. 首先这里介绍一下我们下面用的表结构: author表是保存了作者的个人信息, 因 ...

  2. 将ts文件合并为mp4命令

    cmd: copy/b D:\*.ts D:\new.ts 参考:http://blog.sina.com.cn/s/blog_66b4f1180102uzxs.html

  3. linux 命令——10 cat (转)

    cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. 1.命令格式: cat [选项] [文件] ...

  4. IOS 长按+轻扫(手势识别)

    @interface NJViewController () @property (weak, nonatomic) IBOutlet UIView *customView; @end @implem ...

  5. IOS 一些好的框架和 技术大牛的博客

    http://blog.csdn.net/rodulf/article/details/51871093  比较好的框架链接: http://www.jianshu.com/p/9216c561b0f ...

  6. NOIP2018初赛 解题报告

    前言 \(NOIP2018\)初赛已经结束了,接下来就要准备复赛了. 不过,在此之前,还是先为初赛写一篇解题报告吧. 单项选择题 送分题.(虽然我还是做错了)可以考虑将它们全部转化为\(10\)进制, ...

  7. python_6_if

    _username='qi' _password='abc123' username=input("username:") password=input('password:') ...

  8. cout对象一些常用方法的总结

    cout.precision(n); 这个方法的功能是,设置精度为n,返还值是上一次的设置精度. #include <iostream> using namespace std; int ...

  9. js表单序列化时,非空判断

    在项目中,对于数据的传输一般需要非空的判断,而数据字段较多时一般直接将表单序列化,此时如何判断非空,如下 因为将表单序列化时,数据格式为 trainKind=1&trainKindCode=1 ...

  10. MySQL - 表中某个状态字段的状态表示区分最好用数字,如status - [9999:失败,1111:成功]

    表中某个状态字段的状态表示区分最好用数字,如status - [9999:失败,1111:成功]