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 .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...
随机推荐
- java静态代理和动态代理(一)
代理Proxy: Proxy代理模式是一种结构型设计模式,主要解决的问题是:在直接访问对象时带来的问题. 代理是一种常用的设计模式,其目的就是为其他对象提供一个代理以控制对某个对象的访问.代理类负责为 ...
- I.MX6 7" navigation bar as black bar
/********************************************************************************* * I.MX6 7" n ...
- Jmeter-Critical Section Controller(临界区控制器)
The Critical Section Controller ensures that its children elements (samplers/controllers, etc.) will ...
- VUE的使用方法
vueInit: function() { var _this = this; this.vue = new Vue({ el: '#pa', data: { //存放初始化数据 sourceData ...
- CentOS6.6 VSFTP服务器安装设置
1:安装vsftpd yum install vsftpd 2:关闭防火墙 service iptables stop 3:允许21端口通行 vi /etc/sysconfig/iptables ...
- Phonegap 通信原理
下图为JavaScript调用本地代码的通信过程 Phonegap的核心API都是基于插件的,这些JavaScript API都会调用cordova.exec() 函数来完成操作.cordova.ex ...
- UT报错误:A granted authority textual representation is required
原因:团队唯一标识数据为空,必须保证唯一 牵连需要改进的代码: UserDetailService.java 60行"初始化角色集合"未进行异常处理
- 【linux】mkdir -p命令
如果要创建目录A并创建目录A的子目录B,没有用-p的情况下是mkdir 2次 如果用-p 可以直接创建2个目录 (迭代创建).mkdir -p 目录A/子目录B就可以
- 【pc杂谈】win7系统通过虚拟网卡共享wifi
用管理员权限进入dos命令行 启用并设定虚拟WiFi网卡:netsh wlan set hostednetwork mode=allow ssid=paulnet key=paulwinflo(注意 ...
- MySQL建表规范与常见问题 (go)
一. 表设计 库名.表名.字段名必须使用小写字母,“_”分割. 库名.表名.字段名必须不超过12个字符. 库名.表名.字段名见名知意,建议使用名词而不是动词. 建议使用InnoDB存储引擎. 存储精确 ...