Leetcode dfs Combination SumII
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的更多相关文章
- Leetcode dfs Combination Sum
Combination Sum Total Accepted: 17319 Total Submissions: 65259My Submissions Given a set of candidat ...
- 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 ...
- Leetcode - Letter Combination Of A Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 40. Combination Sum II 组合之和 II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [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 ...
- [LeetCode] 377. Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
随机推荐
- 辛星和你解读PHP递归
事实上递归成其他的编程语言可以是初学者练习功能,但由于PHP特殊性,我们把它拿出来具体解释什么.关于什么是递归先说说.我是第一个承认正在寻求一些递归阶乘,例如,我们写一个函数,然后问多少的阶乘. 看以 ...
- 21个js 技巧收藏
1 Javascript数组转换为CSV格式 首先考虑如下的应用场景,有一个Javscript的字符型(或者数值型)数组,现在需要转换为以逗号分割的CSV格式文件.则我们可以使用如下的小技巧,代码如 ...
- (五岁以下儿童)NS3样本演示:桥模块演示样品csma-bridge.cc凝视程序
(五岁以下儿童)NS3:桥模块演示样品csma-bridge.cc凝视程序 1.Ns3 bridge模csma-bridge.cc演示示例程序的目光 // Network topology // // ...
- iOS开发多线程篇—多线程简介
iOS开发多线程篇-多线程简介 一.进程和线程 1.什么是进程 进程是指在系统中正在执行的一个应用程序 每一个进程之间是独立的.每一个进程均执行在其专用且受保护的内存空间内 比方同一时候打开QQ.Xc ...
- iOS 辛格尔顿
单例模式: 为什么使用单例,单例模式的用途是什么?以下我们举一个样例来诠释一下 举个大家都熟知的样例--Windows任务管理器,如图,我们能够做一个这种尝试,在Windows的"任务栏&q ...
- SQLServer2008-2012资源及性能监控—CPU使用率监控具体解释
前言: CPU是server中最重要的资源.在数据库server中,CPU的使用情况应该时刻监控以便SQLServer一直处于最佳状态. 本文将会使用可靠性和性能监视器来获取CPU相关的使用统计信息 ...
- 微服务API Gateway
翻译-微服务API Gateway 原文地址:http://microservices.io/patterns/apigateway.html,以下是使用google翻译对原文的翻译. 让我们想象一下 ...
- Android文本Flood it游戏源代码
flood_it 流行的Flood it游戏的Android版 游戏玩法: 按下屏幕下方的颜色button,左上角的色块就会变颜色.仅仅要把整片色块变成同色就赢了. 效果图 <ignore_js ...
- Oracle数据表被drop后的恢复
对于被drop的表和索引,都会存放在回收站中(所以对于生产的数据库必须设置好回收站功能) 由于本次生成环境在drop掉已有的表后,又一次创建了很多的表,全部直接还原的话会提示原有对象存在,表名反复.当 ...
- axure & Markman
axure & Markman学习总结 最近学了几款有意思的软件,一款是axure,另一款是Markman.接下来聊聊自己的学习心得吧. 关于axure,百度上的解释是:是一个专业的快速原型设 ...