LeetCode OJ:Combination Sum II (组合之和 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<vector<int>> combinationSum2(vector<int>& candidates, int target) {
tmpCdd = candidates;
sort(tmpCdd.begin(), tmpCdd.end());
vector<int> tmpVec;
dfs(tmpVec, , target);//这个dfs的参数分别是当前vec中含有的元素数目
return result; //序号起始以及,距离target还差的数目
}
private:
vector<int> tmpCdd;
vector<vector<int>> result;
void dfs(vector<int> & tmpVec, int index, int tgt)
{
if(tgt == ){
result.push_back(tmpVec);
return; //达到target,剪枝
}
if(index == tmpCdd.size()) return;
else{
for(int idx = index; idx < tmpCdd.size(); ++idx){
if (idx != index && tmpCdd[idx] == tmpCdd[idx - ])
continue; //这一步的主要目标是防止相邻相同的数和其他数一起匹配成为多个相同的vector,很关键。
if(tmpCdd[idx] <= tgt){
tmpVec.push_back(tmpCdd[idx]); //这其他的实际上和combinationSum1是相同的
dfs(tmpVec, idx + , tgt - tmpCdd[idx]);
tmpVec.pop_back();
}
}
}
}
};
大体就是这样,感觉写的有点乱,想想以后可能再来改。
java版本如下所示,相比上面的写的条理相对的要清楚一点,代码如下所示:
public class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
Arrays.sort(candidates);
for(int i = 0; i < candidates.length; ++i){
List<Integer> curr = new ArrayList<Integer>();
if(i != 0 && candidates[i] == candidates[i-1]) //注意这里和下面同样的地方,防止出现相同的组合
continue;
curr.add(candidates[i]);
getCombination(ret, i + 1, target - candidates[i], curr, candidates);
curr.remove(curr.size() - 1);
}
return ret;
}
public void getCombination(List<List<Integer>> ret, int index, int target, List<Integer> tmpCdd, int [] candidates){
if(index > candidates.length)
return;
if(target < 0){
return;
}else if(target == 0){
ret.add(new ArrayList<Integer>(tmpCdd));
return;
}else{
for(int i = index; i < candidates.length; ++i){
if(i != index && candidates[i] == candidates[i-1])
continue;
tmpCdd.add(candidates[i]);
getCombination(ret, i + 1, target - candidates[i], tmpCdd, candidates);
tmpCdd.remove(tmpCdd.size() - 1);
}
}
}
}
LeetCode OJ:Combination Sum II (组合之和 II)的更多相关文章
- [LeetCode] 377. Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] 216. Combination Sum III 组合之和 III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [LeetCode] 377. Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] 40. Combination Sum II 组合之和 II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [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 39. Combination Sum (组合的和)
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique c ...
- 377 Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
随机推荐
- Ubuntu Linux下通过代理(proxy)使用git上github.com
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/loveaborn/article/details/24575659 github.com.作为程序猿 ...
- shell中的for、while、until(二)
1.C语言格式的for命令: for((var; condition;iteration process)) 注意: 1.给变量赋值可以有空格 2.条件中的变量不以美元符开头: 3.迭代过程的算式未用 ...
- datetime时间处理
基本数据获取: In [38]: import datetime as dt In [39]: on = dt.datetime.now() #获取当前准确时间 In [40]: on Out[40] ...
- form:checkboxes radiobutton select用法
<form:checkboxes path="subjects" items="${requestScope.subjects}" element=&qu ...
- springmvc RequestParam、RequestHeader
/** * 了解: * * @CookieValue: 映射一个 Cookie 值. 属性同 @RequestParam */ @RequestMapping("/testCookieVal ...
- seven habits of highly effective people 高效能人士的七个习惯
习惯的模型 : dependent 依赖 -- independent 独立自主 --interdependent 互相依赖 1: be proactive 主动积极 what you can ...
- 设计模式(六) xml方式实现AOP
1.1. Aop, aspect object programming 面向切面编程 功能: 让关注点代码与业务代码分离! 关注点, 重复代码就叫做关注点: 切面, 关注点形成的类,就叫切面(类) ...
- [国家集训队] calc(动规+拉格朗日插值法)
题目 P4463 [国家集训队] calc 集训队的题目真是做不动呀\(\%>\_<\%\) 朴素方程 设\(f_{i,j}\)为前\(i\)个数值域\([1,j]\),且序列递增的总贡献 ...
- CSS3 3D旋转动画菜单
在线演示 本地下载
- oracle中INSTR函数的用法
今天有个同学问我这个INSTR函数,我也不太清楚就上网查了查做一个小小的记录吧 INSTR(C1,C2,I,J) 在一个字符串中搜索指定的字符,返回发现指定的字符的位置; C1 被搜索的字符串 C2 ...