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. Directx11学习笔记【二十】 使用DirectX Tool Kit加载mesh

    本文由zhangbaochong原创,转载请注明出处:http://www.cnblogs.com/zhangbaochong/p/5788482.html 现在directx已经不再支持.x文件了, ...

  2. js匀速运动停止条件

    匀速运动,怎么让它到达指定位置时停止呢? 原理: 1,物体和目标的差值距离小于等于速度时,即停止 2,接着让物体移动位置等于目标位置 示例:匀速运动停止 html部分 <input type=& ...

  3. or1200中IMMU分析(续)

    下面内容摘自<步步惊芯--软核处理器内部设计分析>一书 2 IMMU中的特殊寄存器 OR1200处理器中的IMMU包括第2组特殊寄存器,如表10.1所看到的. ITLBW0MRx是指令TL ...

  4. 使用python+django+twistd 开发自己的操作和维护系统的一个

    许多开源操作系统和维护系统,例nagios.zabbix.cati等等,但是,当他们得到的时间自己的个性化操作和维护需求,始终无力! 最近的一项研究python.因此,我们认为python+djang ...

  5. JAVA开源爬虫,WebCollector,使用方便,有接口。

    假设你想下载整个网站内容爬行动物,我不希望配置heritrix复杂的爬行动物,要选择WebCollector.项目github一个不断更新. github源地址:https://github.com/ ...

  6. HR筒子说:程序猿面试那点事(转)

    小屁孩曾经有过4年的招聘经验,期间见识了各种类型的程序猿:有大牛.有菜牛:有功成名就,有苦苦挣扎不知方向.等后来做了一枚程序猿之后发现,HR眼中的程序猿和程序猿中的HR都是不一样的.有感与此,从HR的 ...

  7. PHP情人:p十几天来学习hp第一天

    我这里是暂时的 Apache web server 和 MY SQL 如WEB,在php-4.3.3下的环境做的程序.当然要简单的构建和訪问查看数据库 PHPMYADMIN 不可少.  以下简介一下P ...

  8. Spring3 MVC请求参数获取的几种场景

    访问/aaa/bbb所对应的@Controller @RequestMapping("/aaa")//类级别,可以不需要,如果要了,下面所有的请求路径前都需要加入/aaa publ ...

  9. Android - 支持不同的设备 - 支持不同的屏幕

    Android整体上按照两大类来分类设备屏幕:尺寸和分辨率.app在设备上运行应该考虑大小和分辨率.同样的,你应该包含不同的资源来让app适应不同的屏幕大小和分辨率. 整体上有四种尺寸:小的,正常的, ...

  10. Session小案例------完成用户登录

    Session小案例------完成用户登录     在项目开发中,用户登陆功能再平常只是啦,当用户完毕username和password校验后.进入主界面,须要在主界面中显示用户的信息,此时用ses ...