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, a1a2 ≤ … ≤ 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<int> num;
int target;
vector<vector<int> > result;
vector<vector<int> > combinationSum2(vector<int> &num, int target) {
this->num = num;
this->target = target; if(num.size()==){
vector<int> tmp;
result.push_back(tmp);
return result;
} sort(this->num.begin(),this->num.end());
for(int i=;i<num.size();i++){
vector<int> tmp(,this->num[i]);
recursion(i+,tmp,this->num[i]); } return result;
}
private:
void recursion(int start,vector<int> tmp,int sum){
if(sum == target && find(result.begin(),result.end(),tmp)==result.end()){
result.push_back(tmp);
return;
}
vector<int> tmp0(tmp);
int sum0 = sum;
for(int i=start;i<num.size();i++){
tmp.push_back(num[i]);
sum += num[i];
if(sum<target)
recursion(i+,tmp,sum);
else if(sum == target){
if(find(result.begin(),result.end(),tmp)==result.end())
result.push_back(tmp);
}
else
break;
tmp = tmp0;
sum = sum0;
}
}//end func
};

[LeetCode] Combination Sum II (递归)的更多相关文章

  1. LeetCode: Combination Sum II 解题报告

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

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

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

  3. LeetCode——Combination Sum II

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

  4. Leetcode Combination Sum II

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

  5. LeetCode Combination Sum II (DFS)

    题意: 在集合candidates中选出任意多个元素,使得他们的和为target,返回所有的组合,以升序排列. 思路: 难点在于如何去重,比如集合{1,1,2},target=3,那么只有一个组合就是 ...

  6. leetcode Combination Sum II python

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

  7. [Leetcode] combination sum ii 组合之和

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

  8. [Leetcode][Python]40: Combination Sum II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...

  9. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

随机推荐

  1. ural 1437. Gasoline Station

    1437. Gasoline Station Time limit: 1.0 secondMemory limit: 64 MB Once a gasoline meter broke at a fi ...

  2. html页面元素加载顺序

    一般来说,添加背景图片有三种办法: 直接写在标签的style里面,如: <div style="background-image:url('images/Css.JPG')" ...

  3. BZOJ3211 花神游历各国

    Description   Input   Output 每次x=1时,每行一个整数,表示这次旅行的开心度 Sample Input 4 1 100 5 5 5 1 1 2 2 1 2 1 1 2 2 ...

  4. QRadioButton 使用方法

    QRadioButton 控件是Qt中实现多选一功能的控件,它的使用方法如下: 声明控件: QRadioButton *rbutton; 然后实现它的响应函数: void YourClass::on_ ...

  5. web应用防火墙 – 安全伞18.5.1免费版本发布

    “Safe3 Web Application Firewall"是国内安全组织保护伞网络基于新一代Web安全技术开发的全方位企业Web信息安全产品.能有效扫描各种WebShell,同时也可以 ...

  6. 浅谈WPF页间导航

    浅谈WPF页间导航 使用导航的目的是从一个页面进入到另一个页面.无论是预先决定的线性顺序(向导)还是基于层次的用户驱动程序(大部分网站的形式),或者动态生成的路径,主要有3种方法实现:调用Naviga ...

  7. 关于纯css布局的概况

    用一些常用的手法来表现感情或者论证问题,这在XHTML中就是用特定的元素来完成一些常见的信息组织.下面就是信息组织形式与元素的对应列表. img 作为内容的图片是一定要放到img里面的,这没有更好的选 ...

  8. win7(32/64)+apache2.4+php5.5+mysql5.6 环境搭建配置

        引用自:http://blog.csdn.net/z_cf1985/article/details/22454749 环境:win7 32.(64位的同理,下载相关软件必须是对应的64位版本) ...

  9. HDU 1058 优先队列or堆

    本来应当是一道优先队列或者堆的题 因为每个数都应该是已经得到的数*2 *3 *5 *7而得到的 但是 2*7 大于 3*2 这就必须保证每次取得都是没有拿过的最小的数 但是它主动降低难度在样例里卖了个 ...

  10. [ZZ] KlayGE 游戏引擎 之 Order Independent Transparency(OIT)

    转载请注明出处为KlayGE游戏引擎,本文的永久链接为http://www.klayge.org/?p=2233 http://dogasshole.iteye.com/blog/1429665 ht ...