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. 《图书管理系统——java》

    /* (程序头部凝视開始) * 程序的版权和版本号声明部分 * Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved. * 文件名:    < ...

  2. Android复制iPhone日期和时间选择器

    看效果图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGluZ2xvbmd4aW4yNA==/font/5a6L5L2T/fontsize/400/fi ...

  3. [LeetCode55]Jump Game

    题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...

  4. Spark SQL 源代码分析系列

    从决定写Spark SQL文章的源代码分析,到现在一个月的时间,一个又一个几乎相同的结束很快,在这里也做了一个综合指数,方便阅读,下面是读取顺序 :) 第一章 Spark SQL源代码分析之核心流程 ...

  5. shell编程三大神器之grep

  6. mysql数据文件迁移到新的硬盘分区的方法

    该系统增加了一个硬盘.要创建新的分区/data文件夹,mysql对于数据文件夹/var/lib/mysql 1.  停止mysql维修 [root@localhost~]# service mysql ...

  7. JAVA基金会 (三)反射 反思的深度分析

    上一页已经推出反映的一些基本概念,这主要是通过一个例子反映谈的过程,以及样品的实际应用. 这个样例是这种设计思路:从一个属性文件里读取一段字符串,然后,依据该字符串生成相应的类实例对象:这之后另一个增 ...

  8. hdu 4465 概率称号

    http://acm.hdu.edu.cn/showproblem.php?pid=4465 第一直觉概率DP但很快被否定,发现只有一个简单的二项分布,但感情的表达,没有对生命和死亡的例子.然后找到准 ...

  9. html5中关于input使用方法的改变

    測试环境:Firefox 10.0.Safari 5.1.Opera 11.61, Chrome 14.0.835.202 自己測试的时候都有写在form表单里,有提交button验证.由于对博客使用 ...

  10. HTML5 transform三维立方体(随着旋转的效果)

    为了得到更好的把握transform精华.因此,我们决定完成三维立方体模型,可以实现360无死三维旋转作用. 但旋转更难推断每侧视图的序列.然而,完美的解决方案,我希望有人能回答. 源代码直接贡献的朋 ...