题目:

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. JAVA方法定义和调用

    类的方法代表的是实例的某种行为或功能 定义类的方法 访问修饰 类型 方法名(参数列表){ //方法体 } 1.把方法当作一个模块,是个“黑匣子”,完成某个特定的功能,并返回处理结果 2.方法分类“ 返 ...

  2. 转载 tomcat6下项目移植到tomcat7下出问题的解决办法

    转载,原文地址  http://hw1287789687.iteye.com/blog/1817865 org.apache.catalina.core.ContainerBase addChildI ...

  3. Python对Excel操作详解

      Python对Excel操作详解 文档摘要: 本文档主要介绍如何通过python对office excel进行读写操作,使用了xlrd.xlwt和xlutils模块.另外还演示了如何通过Tcl   ...

  4. COGS 750. 栅格网络流

    ★★☆   输入文件:flowa.in   输出文件:flowa.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述] Bob 觉得一般图的最大流问题太难了,他不知道如何解决 ...

  5. javascript模块化---requirejs

    requirejs是异步执行 为什么会出现模块化1.不定什么时候,自己就将全局变量改变了2.函数名的冲突3.依赖关系不好管理如果b.js依赖a.js那么b必须放在a的下面解决的办法1.自执行函数来包装 ...

  6. webpack最简单的入门教程里bundle.js之运行单步调试的原理解析

    读这篇文章的朋友,请确保对webpack有最基础的认识. 您可以阅读我前一篇文章:Webpack 10分钟入门 来在本地运行一个Webpack的hello world项目.https://www.to ...

  7. Intel&amd

  8. [Rails学习之路]Rails文件结构与路由

    约定优于配置和RESTful是Ruby on Rails十分推崇的哲学.在一个默认的RESTful的Rails项目中,使用资源和HTTP动词来帮助组织项目. 假如有一个使用scaffold创建的Rai ...

  9. 实现Hbase的分页

    作者:R星月 出处:http://www.cnblogs.com/rxingyue 欢迎转载,也请保留这段声明.谢谢! 做一个项目中由于数据量比较大,并且需要定时增量分析,做了hbase的分页.项目中 ...

  10. js 封装父页面子页面交互接口

    定义标准接口 Interface= {}; Interface.ParentWin = {}; Interface.ChildWin = {}; /** * 父页面提供的标准接口函数名称 */ Int ...