40. Combination Sum II (Back-Track)
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]
class Solution {
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
backTracking(candidates,,target);
return result;
} void backTracking(vector<int>& candidates, int depth, int target){
if(target==){
result.push_back(item);
return;
}
if(target< || depth >= candidates.size()) return; item.push_back(candidates[depth] );
backTracking(candidates,depth+, target-candidates[depth]);
item.pop_back();
while(++depth<candidates.size() && candidates[depth]==candidates[depth-]);//avoid repetition
backTracking(candidates,depth, target);
}
private:
vector<int> item;
vector<vector<int>> result; };
40. Combination Sum II (Back-Track)的更多相关文章
- [Leetcode][Python]40: Combination Sum II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- 【LeetCode】40. Combination Sum II (2 solutions)
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- [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
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- LeetCode OJ 40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【一天一道LeetCode】#40. Combination Sum II
一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV
▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...
随机推荐
- 关于yo3 所遇到的问题
关于去哪儿开发的yo3 库,实在不敢恭维 ,没有最坑,只有更坑. 官方文档写的实在是 ,有element,iview,ant-design等等一半也可以 ,个人观点. 在使用Scroller中, 自动 ...
- 是用TOP关键字对COUNT性能优化
在对大数据量进行检索或者分页的时候需要计算命中记录数大小,一般情况下我们可以直接COUNT得到结果,但是当结果集很大的时候(比如1万以上)具体结果值已经不重要了.没有人真的翻阅1万条记录,比如百度,你 ...
- nginx默认的配置文件详解
# For more information on configuration, see:# * Official English Documentation: http://nginx.org/en ...
- ImageNet 历届冠军最新评析:哪个深度学习模型最适合你?
原文链接: https://mp.weixin.qq.com/s/I5XgYrPCCGyfV2qTI0sJhQ 深度神经网络自出现以来,已经成为计算机视觉领域一项举足轻重的技术.其中,ImageNet ...
- Oozie_04总结一下workflowf的运行流程【20161116】
4.1 bin/oozie ..... 提交任务 [hadoop@hadoop01 oozie-4.0.0-cdh5.3.6]$ bin/oozie job -oozie http://hadoop0 ...
- 《DSP using MATLAB》Problem 2.10
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- ASP.NET MVC基础入门.
一:ASP.NET MVC 简介 1:asp.net mvc 是一种构建web应用程序的框架,他将一般的MVC(Model--View--Controller)模式应用于asp.net框架. 2:as ...
- vs2005+WinCE模拟器+ActiveSync调试WinCE程序
来源:http://www.cnblogs.com/xjimmyshcn/archive/2011/07/19/2111087.html 一.WinCE 模拟器通过ActiveSync 6.1(即Wi ...
- ELF文件和BIN文件
文件的内容:1. BIN文件是 raw binary 文件,这种文件只包含机器码.2. ELF文件除了机器码外,还包含其它额外的信息,如段的加载地址,运行地址,重定位表,符号表等. 所以ELF文件的体 ...
- UTF-8中的BOM
UTF-8中的BOM UTF-8不需要BOM来表明字节顺序,但可以用BOM来表明编码方式.字符"ZERO WIDTH NO-BREAK SPACE"的UTF-8编码是EF BB B ...