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 ...
随机推荐
- struts2的HelloWorld的基本过程
login.jsp中 <form action="Login"... 该页面提交后, web.xml中设置的过滤器会起作用 <filter> ...
- Touch事件分发机制
原文:http://www.cnblogs.com/linjzong/p/4191891.html Touch事件分发中只有两个主角:ViewGroup和View.Activity的Touch事件事实 ...
- tensorflow 的 tutorial 的卷积神经网络的例子 convolutional.py
具体的网址在这里: https://github.com/tensorflow/tensorflow/tree/r0.12/tensorflow/models 一个卷积神经网络用于股票分析的例子: ...
- webpack基础使用
环境: win10, webpack v3.5.6, node v8.4, npm v5.3. 安装与配置 新建一个项目目录demo, 在当前目录执行如下命令: npm init -y npm ins ...
- Apache和Nigix
Apache , Nginx 是开源的HTTP服务器软件 HTTP服务器本质上也是一种应用程序--它通常运行在服务器之上,绑定服务器的IP地址并监听某一个tcp端口来接收并处理HTTP请 ...
- js获取iframe和父级之间元素,方法、属,获取iframe的高度自适应iframe高度
摘自:http://blog.csdn.net/kongjiea/article/details/38870399 1.在父页面 获取iframe子页面的元素 (在同域的情况下 且在http://下测 ...
- Atom常用快捷键及设置
常用快捷键: cmd-z 修改历史中后退 cmd-y 修改历史中前进 ctl-shift-c 复制该聚焦文件的路径 cmd-/ 注释 shift-cmd-d 复制整行并粘贴到下一行 ctl-shift ...
- gitlab + jenkins + docker + k8s
总体流程: 在开发机开发代码后提交到gitlab 之后通过webhook插件触发jenkins进行构建,jenkins将代码打成docker镜像,push到docker-registry 之后将在k8 ...
- @RequestBody和@ResponseBody的使用情形以及RestTemplate的http报文转换
@RequestBody和@ResponseBody两个注解,分别完成请求报文到对象和对象到响应报文的转换. @RequestBody 1.@requestBody注解常用来处理content-typ ...
- contain与compareDocumentPosition
contain方法由IE创建,用于判断元素之间是否是父亲与后代的关系,例如:如果A元素包含B元素,则返回true,否则,返回false eg: <div id= "a"> ...