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. iphone6来了,我该做点什么(兼容iphone6的方法)

    北京时间2014年9月10日凌晨1点,苹果公司正式发布其新一代产品 iPhone6,相信做webapp开发的同学对它是充满了好奇和等待,也担心它带来各种坑爹,高清的分辨率,升级的retina显示屏,我 ...

  2. TYVJ P1024 外星人的密码数字

    做题记录:2016-08-16 20:09:30 描述     XXXX年突然有外星人造访,但大家语言不通,不过科学家们经过研究发现外星人用26个英文字母组成的单词中最长不降子序列的长度来表述数字,且 ...

  3. 【POJ】2449 Remmarguts' Date(k短路)

    http://poj.org/problem?id=2449 不会.. 百度学习.. 恩. k短路不难理解的. 结合了a_star的思想.每动一次进行一次估价,然后找最小的(此时的最短路)然后累计到k ...

  4. shell总结(0基础入门)

    一.简介 shell是用户和操作系统交互的命令行解释器. shell有很多种: bash.csh.sh.ksh... 我们等了linux时看到的命令行就是一个bash. 二.第一个脚本: [root@ ...

  5. obout editor Absolute path for uploaded image

    本文转自:https://www.obout.com/editor_new/KnowledgeBase.aspx?id=706   Absolute path for uploaded image Q ...

  6. 什么是java 键值对

    所谓键值对,你可以查看jdk文档,找MAP接口,它的实现类都是键值对的形式保存数据的 键:就是你存的值的编号值:就是你要存放的数据

  7. pod 安装 Masonry 遇到问题

    pod 导入第三方库 Masonry: 在工程masonryTest的文件下新建一个Podfile文件 编辑如下内容: platform :ios, '8.0'xcodeproj 'mansoryTe ...

  8. GTX780

  9. PHP 设计模式 笔记与总结(2)开发 PSR-0 的基础框架

    [PSR-0 规范的三项约定]: ① 命名空间必须与绝对路径一致 ② 类名的首字母必须大写 ③ 除入口文件外,其他".php"必须只有一个类(不能有可执行的代码) [开发符合 PS ...

  10. MySQL解压版安装配置

    官网下载地址:http://dev.mysql.com/downloads/windows/installer/ (可以选择解压版zip下载,也可以选择msi安装版.) 解压zip版配置: 1. 下载 ...