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)的更多相关文章

  1. [LeetCode] 377. Combination Sum IV 组合之和 IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  2. [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 ...

  3. [LeetCode] 377. Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  4. [LeetCode] 40. Combination Sum II 组合之和 II

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  5. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  6. [LeetCode] Combination Sum III 组合之和之三

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  7. LeetCode 39. Combination Sum (组合的和)

    Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique c ...

  8. 377 Combination Sum IV 组合之和 IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  9. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

随机推荐

  1. PHP使用Mongodb

    一.安装Mongodb的PHP扩展 wget http://pecl.php.net/get/mongo-1.2.7.tgz //下载扩展包tar zxvf mongo-1.2.7.tgzcd mon ...

  2. 002-主流区块链技术特点及Hyperledger Fabric V0.6版本特点

    一.主流区块链技术特点 二.HyperLedger子项目 三.Hyperledger fabric架构 V0.6逻辑架构: V0.6区块链网络 对应的0.6版本的运行时架构: 0.6版本的架构特点是: ...

  3. cmd命令行和bat批处理操作windows服务(转载)

    一.cmd命令行---进行Windows服务操作 1.安装服务 sc create 服务名 binPath= "C:\Users\Administrator\Desktop\win32srv ...

  4. java队列的实现

    队列也可以通过数组和链表两种方式来实现. 1.链表方式实现 class Node{ Node next = null; int data; public Node(int data){this.dat ...

  5. oracle中记录被另一个用户锁住的原因与解决办法

    oracle数据中删除数据时提示“记录被另一个用户锁住” 解决方法: 1.查看数据库锁,诊断锁的来源及类型: select object_id,session_id,locked_mode from ...

  6. python之路 前段之html,css

    一.HTML 超级文本标记语言是标准通用标记语言下的一个应用,也是一种规范,一种标准, 它通过标记符号来标记要显示的网页中的各个部分.网页文件本身是一种文本文件,通过在文本文件中添加标记符,可以告诉浏 ...

  7. CodeForces - 987D Fair (BFS求最短路)

    题意:有N个城市,M条双向道路连接两个城市,整个图保证连通.有K种物品,但每个城市只有一种,现在它们都需要S种物品来举办展览,可以去其他城市获取该城市的物品,花费是两城市之间的最短路径长度.求每个城市 ...

  8. HDU - 6333 Problem B. Harvest of Apples (莫队+组合数学)

    题意:计算C(n,0)到C(n,m)的和,T(T<=1e5)组数据. 分析:预处理出阶乘和其逆元.但如果每次O(m)累加,那么会超时. 定义 S(n, m) = sigma(C(n,m)).有公 ...

  9. 【转】PCA与Whitening

    PCA: PCA的具有2个功能,一是维数约简(可以加快算法的训练速度,减小内存消耗等),一是数据的可视化. PCA并不是线性回归,因为线性回归是保证得到的函数是y值方面误差最小,而PCA是保证得到的函 ...

  10. python 异常的引发和捕捉处理

      1.什么是异常(exception): 异常是python发现某个地方出现逻辑错误时,抛出一个信号,即异常的引发.如果有捕捉语句在,则异常信号被捕捉,如果没有则会传递到默认异常处理器(终止程序). ...