[Leetcode] combination sum ii 组合之和
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 (a 1, a 2, … , a k) must be in non-descending order. (ie, a 1 ≤ a 2 ≤ … ≤ a k).
- The solution set must not contain duplicate combinations.
For example, given candidate set10,1,2,7,6,1,5and target8,
A solution set is:
[1, 7]
[1, 2, 5]
[2, 6]
[1, 1, 6]
题意:给定值T,在C中找到和为T的组合,要求:C中的每个元素只能出现一次,不能用有重复组合。
思路:正确理解,C中的每个元素只能出现一次!如例子中的[1,1,6],只是数组中的每个元素只能出现一次,不是说,元素值相等的只能出现一次。要给出所以满足条件的解,这题是典型的深搜。在深搜之前要注意到题中要求,每个组合中元素的值要是非降序的,所以,要先对数组进行排序。因为数组中元素的值有重复的情况,所以在写DFS函数时,要跳过重复的元素。参考了Grandyang的写法。比如排序以后,题中例子变为[1,1,2,5,6,7,10],解中给出以第一个元素1和7组合的解了,以第二元素1和7的解要跳过。这时也不过跳过由重复数字组成的解,因为,首次出现重复的数字时,已经给出了由重复数字组合等于给定值的情况。
代码如下:
class Solution {
public:
vector<vector<int> > combinationSum2(vector<int> &num, int target)
{
vector<vector<int>> res;
vector<int> midVal;
sort(num.begin(),num.end());
DFS(res,midVal,num,target,);
return res;
}
void DFS(vector<vector<int>> &res,vector<int> &midVal,vector<int> &num,int target,int start)
{
if(target<)
return;
else if(target==)
res.push_back(midVal);
else
{
for(int i=start;i<num.size();i++)
{
if(i>start&&num[i]==num[i-]) //
continue;
midVal.push_back(num[i]);
DFS(res,midVal,num,target-num[i],i+);
midVal.pop_back();
}
}
}
};
[Leetcode] combination sum ii 组合之和的更多相关文章
- [LeetCode] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Combination Sum III 组合之和之三
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [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 OJ:Combination Sum II (组合之和 II)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] 40. Combination Sum II 组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 377. Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] 377. Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
随机推荐
- PS 旋转任意角度的照片
1.选择标尺工具 2.在图片上画一个线,然后工具栏--图像--图像旋转
- java 泛型历史遗留问题
Map<String,Integer> hashMap = new HashMap<String,Integer>(); hashMap.put(); // hashMap.p ...
- 「LeetCode」0003-Add Two Numbers(Typescript)
分析 代码 /** * @param {ListNode} l1 * @param {ListNode} l2 * @return {ListNode} */ var addTwoNumbers=fu ...
- OSG-获取OSG的源代码和第三方库并编译
获取OSG的源代码有很多方式. 这里说下其中的两个地方,第一就是中国的OSG网站http://www.osgchina.org/,这个网站目前应该是由中国西安恒歌科技维护,同时,西安恒歌科技也是一家已 ...
- Linux命令应用大词典-第13章 用户和组群管理
13.1 useradd:创建用户账户 13.2 adduser:创建用户账户 13.3 lnewusers:创建用户账户 13.4 usermod:修改用户账户 13.5 userdel:删除用户账 ...
- php+原生ajax实现图片文件上传功能实例
html+js 代码 <!DOCTYPE html> <html> <head> <title>Html5 Ajax 上传文件</title> ...
- Fox and Number Game
Fox Ciel is playing a game with numbers now. Ciel has n positive integers: x1, x2, ..., xn. She can ...
- c# CLR无法从 COM 上下文 0x51cd20 转换为 COM 上下文 0x51ce90
调试菜单--->异常---->managed debugging assistants栏下ContextSwitchDeadlock 前面的√去掉
- JTS空间分析工具包(GIS开源)学习 JAVA
JST空间分析工具包是一套JAVA API,提供一系列的空间数据分析操作.最近开发项目刚好需要用到,上网搜资料也少,就自己写下来记录一下.C++版本的拓扑分析开源工具叫:geos:.NET版本的拓扑分 ...
- YaoLingJump开发者日志(一)
写完PokeCats之后意犹未尽,还想做一个更加有趣的游戏,比如说像超级玛丽那样. 游戏的主角就选"瑶玲"了,这是我小时候最喜欢的动画片<瑶玲啊瑶玲>的女主角. ...