[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 (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<int> num;
int target;
vector<vector<int> > result;
vector<vector<int> > combinationSum2(vector<int> &num, int target) {
this->num = num;
this->target = target; if(num.size()==){
vector<int> tmp;
result.push_back(tmp);
return result;
} sort(this->num.begin(),this->num.end());
for(int i=;i<num.size();i++){
vector<int> tmp(,this->num[i]);
recursion(i+,tmp,this->num[i]); } return result;
}
private:
void recursion(int start,vector<int> tmp,int sum){
if(sum == target && find(result.begin(),result.end(),tmp)==result.end()){
result.push_back(tmp);
return;
}
vector<int> tmp0(tmp);
int sum0 = sum;
for(int i=start;i<num.size();i++){
tmp.push_back(num[i]);
sum += num[i];
if(sum<target)
recursion(i+,tmp,sum);
else if(sum == target){
if(find(result.begin(),result.end(),tmp)==result.end())
result.push_back(tmp);
}
else
break;
tmp = tmp0;
sum = sum0;
}
}//end func
};
[LeetCode] Combination Sum II (递归)的更多相关文章
- LeetCode: Combination Sum II 解题报告
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- [LeetCode] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- LeetCode——Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- Leetcode Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- LeetCode Combination Sum II (DFS)
题意: 在集合candidates中选出任意多个元素,使得他们的和为target,返回所有的组合,以升序排列. 思路: 难点在于如何去重,比如集合{1,1,2},target=3,那么只有一个组合就是 ...
- leetcode Combination Sum II python
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [Leetcode] combination sum ii 组合之和
Given a collection of candidate numbers ( C ) and a target number ( T), find all unique combinations ...
- [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 ...
随机推荐
- BZOJ2190 [SDOI2008]仪仗队(欧拉函数)
与HDU2841大同小异. 设左下角的点为(1,1),如果(1,1)->(x,y)和(1,1)->(x',y')向量平行,那只有在前面的能被看见.然后就是求x-1.y-1不互质的数对个数. ...
- HDU2841 Visible Trees(容斥原理)
题目..大概就是有个m*n个点的矩形从(1,1)到(m,n),问从(0,0)出发直线看过去最多能看到几个点. 如果(0,0)->(x,y)和(0,0)->(x',y')两个向量平行,那后面 ...
- JSTL 的 if else : 有 c:if 没有 else 的处理
jstl的c:if 没有else 想要实现if...else...: 可以用下面的解决 -------------------------------------------------------- ...
- BZOJ3807 : Neerc2011 Lanes
左右与右左是两个独立的问题 设f[i]表示i时刻左右车道减少一条的答案 g[i]表示i时刻右左车道增加一条的答案 ans=min(f[i]+g[i+r]) 计算f[i]: 首先暴力计算出f[m+1], ...
- Javascript history pushState onpopstate方法做AJAX SEO
参考MDN: https://developer.mozilla.org/zh-CN/docs/DOM/Manipulating_the_browser_history https://develop ...
- MONO 安装 分析
你是安装在/etc下的吧? 5.2是没有serverbusy的提示的,那时,它就傻等,给人造成down的假像.而现在的版本,会提示的. 你升级时,upgrade后边加参数了吗? 加了 /etc/jw ...
- [开源框架推荐]Icepdf:纯java的pdf文档的提取和转换库
ICEpdf 是一个轻量级的开源 Java 语言的 PDF 类库.通过 ICEpdf 可以用来浏览.内容提取和转换 PDF 文档,而无须一些本地PDF库的支持. 可以用来做什么? 1.从pdf文件中提 ...
- JavaScript 导出Excel 代码
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...
- [转].net中的认证(authentication)与授权(authorization)
本文转自:http://www.cnblogs.com/yjmyzz/archive/2010/08/29/1812038.html 注:这篇文章主要给新手看的,老手们可能会觉得没啥营养,就请绕过吧. ...
- ITIL图示