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 .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...
随机推荐
- CUDA Samples: matrix multiplication(C = A * B)
以下CUDA sample是分别用C++和CUDA实现的两矩阵相乘运算code即C= A*B,CUDA中包含了两种核函数的实现方法,第一种方法来自于CUDA Samples\v8.0\0_Simple ...
- 关于EPoll的个人理解
1.epoll 是I/o多路复用的一种解决方案,对比select的优点有: a.支持打开最大的文件描述符(可高达百万) b.效率并不随着描述符的增多而线性下降.select每次是轮询,所以描述符越多效 ...
- rancher下的kubernetes之二:安装rancher和kubernetes
在上一章<rancher下的kubernetes之一:构建标准化vmware镜像>,我们做了个通用的虚拟机镜像,可以root登录,apt已经更新,docker也装好了,现在我们就来安装ra ...
- iMac 10年
10年前,也就是1998年8月15号,Apple 推出了蓝色半透明的 iMac G3(指上市,发布时间为当年5月6号),当年销售200万台,从此开启了它的一个时代,人们一旦说起设计,那么 Apple ...
- Swift学习——A Swift Tour 协议和扩展
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhenyu5211314/article/details/28854395 Protocols an ...
- phpmyadmin不允许一个表创建多个主键的解决办法
在phpmyadmin中执行建表语句 CREATE TABLE `user3` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(2 ...
- SolidWorks 导出工程图时流程
SolidWorks 导出工程图时流程 新建零件装配件制作工程图 设置比例 以前经验发现很我输出的图纸比例不对,需要先设置. 空白处右键,进入属性. 设置图纸比例为 1:1.
- sql update set使用case when语句
1. update TD_XXXsetdjyzmdm=null,djyzmsj=null,DLCS= case when DLCS is null then 1 else DLCS+1 end whe ...
- 5.Appium+真机Demo
1.连接真机后,执行代码时出现错误:A new session could not be created. (Original error: Could not extract PIDs from p ...
- 'scalar deleting destructor' 和 'vector deleting destructor'的区别
在用到delete的时候,我们往往会针对类对象与类对象数组做不同删除,在这背后编译器是如何做的? #include<iostream> using namespace std; class ...