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 ...
随机推荐
- 使用Berkeley Parser进行句法分析
一.句法分析 1.定义 句法分析判断输入的单词序列(一般为句子)的构成是否合乎给定的语法,并通过构造句法树来确定句子的结构以及各层次句法成分之间的关系,即确定一个句子中的哪些词构成一个短语,哪些词是动 ...
- C#检测两个文件内容是否相同
不知道为什么对Excel 2010 xlsx后缀的文件没有效果,求解! 对其他文件有效,如.txt,.csv using System; using System.Security.Cryptogra ...
- python数据类型及其操作
一.数字 常用类型:int,float age = 10 # int型 salary = 3000.5 # float型 进制: 二进制: 11 = 1*21 + 1*20 = 3 八进制: 11 ...
- ubuntu服务器无法运行chromedriver解决方法(转)
无头浏览器 sudo apt-get install Xvfb sudo pip install pyvirtualdisplay from pyvirtualdisplay import Displ ...
- springboot-vue项目后台2---pojo对查询结果手动分组
<resultMap id="PResult" type="packs" > <result column="device_type ...
- Linux安全策略配置-pam_tally2身份验证模块
PAM身份验证安全配置实例 - 强制使用强密码(用户密码安全配置) - 用户SSH登录失败尝试次数超出限制后锁定账户(帐户锁定/解锁和时间设置) - 允许普通用户使用sudo而不是su(限制普通用户登 ...
- MySQL-5.7 Update语句详解
1.语法 (1)单表 UPDATE [LOW_PRIORITY] [IGNORE] table_reference SET assignment_list [WHERE where_condition ...
- mysql数据库导入、导出、数据传输
Navicat数据库之间导入导出1.双击要导出的数据库,右键选转储SQL文件...,选择要保存的文件夹. 2.点击开始后,开始导出. 数据库导入1.新建数据库,数据库的名字必须和导入的数据库文件一致. ...
- Linux网络配置脚本
#!/bin/bash ip=$1 if [ -f "/etc/sysconfig/network-scripts/ifcfg-bond1" ] then break else # ...
- MySQL5.6一键部署
# 快速部署单节点MySQL脚本# 执行方式:将MySQL包,my3302.cnf配置文件,dbtool脚本文件放到一个目录下.然后按照脚本执行就可以.配置文件提前修改好.文章最后附上my.cnf配置 ...