leetcode - 40. Combination Sum II - Medium

descrition

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.
  • 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]
]

解析

leetcode - 39. Combination Sum - Medium 类似,只是这里的数组元素存在重复,并且元素不可重复取。

代码只实现了其中一种递归形式,这样的实现方法递归层数应该是最浅的。

code


#include <iostream>
#include <vector>
#include <algorithm> using namespace std; class Solution{
public:
vector<vector<int> > combinationSum2(vector<int> &candidates, int target){
vector<vector<int> > ans;
vector<int> vecCur;
sort(candidates.begin(), candidates.end());
combinationSum2Backtracking(candidates, 0, vecCur, target, ans);
return ans;
} void combinationSum2Backtracking(vector<int>& candidates, int index,
vector<int>& vecCur, int target,
vector<vector<int> > &ans){
if(target < 0)
return;
if(target == 0){
if(!vecCur.empty())
ans.push_back(vecCur);
return;
} for(int i=index; i<candidates.size(); i++){
if(candidates[i] > target) // candidates mush in ascending order
break;
// choose candidates[i], and each number in candidates may only
// be used onece in combination
vecCur.push_back(candidates[i]);
combinationSum2Backtracking(candidates, i+1, vecCur, target - candidates[i], ans);
// don't choose candidates[i]
vecCur.pop_back(); // skip the duplicate
while((i+1)<candidates.size() && candidates[i+1] == candidates[i])
i++;
// after i++, i will point to a new unique number
}
}
}; int main()
{
return 0;
}

[array] leetcode - 40. Combination Sum II - Medium的更多相关文章

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

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

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

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

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

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

  4. LeetCode 40. Combination Sum II (组合的和之二)

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  5. Leetcode 40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  6. leetcode 40 Combination Sum II --- java

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  7. [LeetCode] 40. Combination Sum II ☆☆☆(数组相加等于指定的数)

    https://leetcode.wang/leetCode-40-Combination-Sum-II.html 描述 Given a collection of candidate numbers ...

  8. Java [Leetcode 40]Combination Sum II

    题目描述: Given a collection of candidate numbers (C) and a target number (T), find all unique combinati ...

  9. LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)

    题目链接:https://leetcode.com/problems/combination-sum-ii/?tab=Description   给定数组,数组中的元素均为正数,target也是正数. ...

随机推荐

  1. get和post请求及函数调用模式

    1.get和post请求的应用场景? get: 1.get请求获取(查询)数据 2.请求url长度比较短 3.可以被缓存 4.请求url可以作为浏览器书签 5.可以被保存在浏览器记录中 6.请求参数在 ...

  2. Transform组件以及相关API

    Transform.Translate(Vector3,Space):以自身坐标系或者世界坐标系向某个方向移动物体. Vector3:表示方向和移动的距离. Space:空间.枚举类型,用来确定坐标系 ...

  3. cardview和Palette,ActionBar颜色随图改变

    CardView是一个控件,Palette是取色工具(工具类),本文会对他们进行比较细致的介绍,相信机制的各位看完一定轻而易举地实现ActionBar随图改变的特效. 首先看一下效果图: Gradle ...

  4. 08-图8 How Long Does It Take

    原题: Given the relations of all the activities of a project, you are supposed to find the earliest co ...

  5. 晓莲说-何不原创:如何通过jad把class批量反编译成java文件

    背景:前几天在项目开发的时候遇到一个问题,那就是利用myeclipse编写好的一个项目打包成jar包后上传部署到服务器里,之后本地的项目被自己改来改去出现了一些问题,想着把上传到服务器里面的war包下 ...

  6. memcache 启动 储存原理 集群

    一. windows下安装启动 首先将memcache的bin目录加入到Path环境变量中,方便后面使用命令: 然后执行 memcached –dinstall 命令安装memcache的服务: 然后 ...

  7. ibv_open_device()函数

    struct ibv_context *ibv_open_device(struct ibv_device *device); 描述 函数会创建一个RDMA设备相关的context:可以通过ibv_c ...

  8. JMeter接口测试系列-关联参数

    这里主要记录一下A接口的返回结果经过md5加密之后作为另外B接口的参数,这个问题困扰了很久,找了不少资料,现在把解决方法记录如下: 环境 ①JMeter 3.0 ②前置条件:将fastjson.jar ...

  9. 【POJ3254】Corn Fields

    http://poj.org/problem?id=3254 题意:给你一块n*m(0<n,m<=12)的地图,其中有的方格是肥沃的(用1表示),有的方格是贫瘠的(用0表示).现在约翰要在 ...

  10. MySql的隔离级别和锁的关系

    一.事务的4个基本特征  Atomic(原子性):  事务中包括的操作被看做一个逻辑单元.这个逻辑单元中的操作要  么所有成功.要么所有失败. Consistency(一致性):  仅仅有合法的数据能 ...