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. 《java.util.concurrent 包源码阅读》08 CopyOnWriteArrayList和CopyOnWriteArraySet

    CopyOnWriteArrayList和CopyOnWriteArraySet从数据结构类型上来说是类似的,都是用数组实现的保存一组数据的数据结构,区别也简单就是List和set的区别.因此这里就先 ...

  2. Python学习笔记 变量

    蒟蒻高举横幅:部分内容转自廖雪峰的Python教学 1.Python是动态语言,即它的变量是没有类型的. !/usr/bin/env python a = 'ABC' print a a = 123 ...

  3. laravel中with()方法,has()方法和whereHas()方法的区别

    with() with()方法是用作"渴求式加载"的,那主要意味着,laravel将会伴随着主要模型预加载出确切的的关联关系.这就对那些如果你想加在一个模型的所有关联关系非常有帮助 ...

  4. 加密代理和Retrofit解密Converter

    最近在研究安卓的Retrofit框架,服务器的数据全部用加密算法加密了,发现无法使用"com.squareup.retrofit2:converter-gson:2.1.0"Jar ...

  5. 自定义控件,上图下字的Button,图片任意指定大小

    最近处在安卓培训期,把自己的所学写成博客和大家分享一下,今天学的是这个自定义控件,上图下字的Button安卓自带,但是苦于无法设置图片大小(可以在代码修改),今天自己做了一个,首先看一下效果图,比较实 ...

  6. C语言之for循环

    #include<stdio.h>#include<stdlib.h>#include<time.h>int main(){ int i; for(i=1;i< ...

  7. UEditor1.4.3.3实现图片上传、删除功能

    1.下载ueditor1.4.3.3 UTF-8的版本 2.新建一个项目,在项目中添加UEditor,把下载好的插件都放在ueditor这个文件夹中,在进行一些基本的配置 3.在根目录下新建一个为in ...

  8. 【java系列】java开发环境搭建

    描述 本篇文章主要讲解基于windows 10系统搭建java开发环境,主要内容包括如下: (1)安装资料准备 (2)安装过程讲解 (3)测试是否安装成功 (4)Hello Word测试 1   安装 ...

  9. memcache 启动 储存原理 集群

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

  10. Linux磁盘分区(二):删除

    ***********************************************声明************************************************ 原创 ...