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 ...
随机推荐
- 012-JDK可视化监控工具-jstack
一.概述 jstack是java虚拟机自带的一种堆栈跟踪工具.jstack用于打印出给定的java进程ID或core file或远程调试服务的Java堆栈信息,如果是在64位机器上,需要指定选项&qu ...
- ubuntu16.04 tomcat7安装和编码修改(转发:https://blog.csdn.net/zl544434558/article/details/76735564)
有直接通过命令安装的,但是我还是喜欢把文件下载下来,然后自己配置. 1,下载tomcat7二进制文件 https://tomcat.apache.org/download-70.cgi 2,解压tom ...
- 用python实现一个计算器
import re def atom_cal(exp): # 计算乘除法 if '*' in exp: a,b = exp.split('*') return str(float(a) * float ...
- Java 基础总结(一)
本文参见:http://www.cnblogs.com/dolphin0520/category/361055.html 1. String,StringBuffer,StringBuilder 1) ...
- iOS应用内付费(IAP)开发步骤
1.苹果iTunes Connect内购产品信息录入. 1)创建app内购买项目(Create New),选择类型: 1.消耗型项目 对于消耗型 App 内购买项目,用户每次下载时都必须进行购买.一次 ...
- jmeter常用插件安装
转载:http://www.cnblogs.com/danqiu/p/6119156.html 下载地址:http://jmeter-plugins.org/downloads/all/ PerfMo ...
- bind方法代替闭包
<a href="JavaScript:">a</a> <a href="JavaScript:">b</a> ...
- C#中各种数据类型可以表示的数据的范围
C#中各种数据类型可以表示的数据的范围 BOOL型为int型,一般认为占4个字节,取值TRUE/FALSE/ERROR. sbyte型为有符号8位整数,占1个字节,取值范围在-128 - 127之间. ...
- JavaScript传递的是引用的副本
看例子 var test1 = function (args) { args.name = "lcc2"; args = { name: "lcc3" }; } ...
- Python3.x:Linux下安装python3.6
Python3.x:Linux下安装python3.6 下载 #先进入download文件夹 cd /home/download #输入命令(下载到当前目录) wget https://www.pyt ...