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. Machine Schedule

    Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. unity button

    #pragma strict var buttonTexture:Texture2D; private var str:String; private var frameTime:int; funct ...

  3. Vijos 1180 (树形DP+背包)

    题目链接: https://vijos.org/p/1180 题目大意:选课.只有根课选了才能选子课,给定选课数m, 问最大学分多少. 解题思路: 树形背包.cost=1. 且有个虚根0,取这个虚根也 ...

  4. Java集合的线程安全用法

    线程安全的集合包含2个问题 1.多线程并发修改一 个 集合 怎么办? 2.如果迭代的过程中 集合 被修改了怎么办? a.一个线程在迭代,另一个线程在修改 b.在同一个线程内用同一个迭代器对象进行迭代. ...

  5. 密码等级:至少包含字母、大小写数字、字符中的两种 JS实现方案

    前言 密码,如果设置的太简单,很容易就被攻破,所以很多网站将密码设置的要求设置的挺严格,一般是字母.数字.字符3选2,区分大小写.对于设置得太简单的密码,予以错误提示.或者予以密码等级(低中高)显示, ...

  6. web farm 讨论引出

    关于web farm 有成功的实施的文档没 用它还不如 用nginx,简单易用. Nginx for windows的运行效果咋样 windows  iis无敌 玩nginx就不要用win系统,必须l ...

  7. POJ 2891 Strange Way to Express Integers(中国剩余定理)

    题目链接 虽然我不懂... #include <cstdio> #include <cstring> #include <map> #include <cma ...

  8. js控制页面的全屏展示和退出全屏显示

    <!DOCTYPE html> <html> <meta http-equiv="Content-Type" content="text/h ...

  9. C# - Lee 公共类库

    我的公共类库 using System; using System.IO; using System.Net; using System.Security.Cryptography; using Sy ...

  10. Linux Path文件夹内容