Combination Sum II

Total Accepted: 13710 Total
Submissions: 55908My Submissions

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]

题意:给定一组数C和一个数值T,在C中找到全部总和等于T的组合。

C中的同一数字最多仅仅能拿一次。找到的组合不能反复。

思路:dfs

第i层的第j个节点有  n - i - j 个选择分支

递归深度:递归到总和大于等于T就能够返回了

复杂度:时间O(n!),空间O(n)

vector<vector<int> > res;
vector<int> _num;
void dfs(int start, int target, vector<int> &path){
if(target == 0) {res.push_back(path); return;}
int previous = -1; //这里要加上这个来记录同一层分枝的前一个值。假设当前值跟前一个值一样。就跳过,避免反复
for(int i = start; i < _num.size(); ++i){
if(previous == _num[i]) continue;
if(target < _num[i]) return; //剪枝
previous = _num[i];
path.push_back(_num[i]);
dfs(i + 1, target - _num[i], path);
path.pop_back();
}
}
vector<vector<int> > combinationSum2(vector<int> &num, int target){
_num = num;
sort(_num.begin(), _num.end());
vector<int> path;
dfs(0, target, path);
return res;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

Leetcode dfs Combination SumII的更多相关文章

  1. Leetcode dfs Combination Sum

    Combination Sum Total Accepted: 17319 Total Submissions: 65259My Submissions Given a set of candidat ...

  2. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  3. Leetcode - Letter Combination Of A Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

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

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

  5. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  6. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  7. [LeetCode] 40. Combination Sum II 组合之和 II

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  8. [LeetCode] 216. Combination Sum III 组合之和 III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  9. [LeetCode] 377. Combination Sum IV 组合之和 IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

随机推荐

  1. Metatable和Metamethod(转)

    Metatable和Metamethod是用来干啥的?它们可以使得表a和b的表达式“a + b”变得有意义,其中metatable使两个不相关的表a和b之间可以进行操作,而操作的具体行为比如说&quo ...

  2. Android - 分享内容 - 接收其他APP的内容

    就象程序可以发送数据给其他程序,所以也可以接收其他程序的数据.想一下用户如何和程序交互,以及想从其他程序接收什么样类型的数据.例如,一个社交程序可能对接收其他程序的文字(比如有趣的网址)感兴趣.Goo ...

  3. 【Java基础】对象的具体创建过程

    所有的类(以Dog类为例)在第一次使用时,动态的加载到JVM中,当首次创建Dog对象时,或者是Dog类的静态方法.静态属性域在第一次被访问时,JVM解释器查找到classpath,定位到Dog.cla ...

  4. OpenGL学习日记-2015.3.13——多实例渲染

        实例化(instancing)或者多实例渲染(instancd rendering)是一种连续运行多条同样渲染命令的方法.而且每一个命令的所产生的渲染结果都会有轻微的差异. 是一种很有效的.有 ...

  5. 基于OpenCV性别识别

    叙述性说明 所谓的性别识别推断检测到的面部是男性还是女性.它是一个二值分类问题. 识别算法可以用于SVM,BP神经网络.LDA,PCA,PCA+LDA等等.OpenCV官网给出的文档是基于Fisher ...

  6. RH253读书笔记(1)-Lab 1 System Monitoring

    Lab 1 System Monitoring Goal: To build skills to better assess system resources, performance and sec ...

  7. Android 中字体的处理

    //得到TextView控件对象 TextView textView = (TextView)findViewById(R.id.custom); //将字体文件保存在assets/fonts/文件夹 ...

  8. 探索static——不需要能够使用该类实例?

    在一般情况下.需要使用一个上课时间.你必须先实例化类,它调用的能力.在编程过程中发现.有些类不能直接实例来使用,利用其场.法等等. 这时候.靠的就是static作用.static英文意思为" ...

  9. QlikView线图高亮选择尺寸

    作为标题,如今,学生问我一个问题.尺寸Month.expression它是Count(Id). 这个图是一个折线图,不管你选择哪个月的其他下拉列表,销售量.由于Expression里面是这样写的 Co ...

  10. JBPM4实例教程

    JBPM语言概述:全名  Java Business Process Management  ,它是覆盖了业务流程管理.工作流.服务协作等领域的一个开源的.灵活的.易扩展的可运行流程语言框架. 是开源 ...