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. key-value存储数据库--Redis

    1.简介 Redis是完全开源的ANSI C语言编写.遵守BSD协议,高性能的key-value数据库. 1.1特点 Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载 ...

  2. openFace 人脸识别框架测试

    openface  人脸识别框架  但个人感觉精度还是很一般 openface的githup文档地址:http://cmusatyalab.github.io/openface/ openface的安 ...

  3. ExceptionLess 本地部署小结

    ExceptionLess 是一个免费开源分布式系统日志收集框架,地址:https://github.com/exceptionless/Exceptionless 运行环境: .NET 4.6.1 ...

  4. 在没有DOM操作的日子里,我是怎么熬过来的(中)

    前言 继上篇推送之后,在掘金.segmentfault.简书.博客园等平台上迅速收到了不俗的反馈,大部分网友都留言说感同身受,还有不少网友追问中篇何时更新.于是,闰土顺应呼声,在这个凛冽的寒冬早晨,将 ...

  5. C++ UTF8和GB2312相互转换

    #include <Windows.h> #include <string> using std::string; void UTF8_to_GB2312(const char ...

  6. Winform控件Tag使用规范

    背景 Tag在WinForm控件中经常被用来存储临时数据,类型为object,但是当程序中多个地方使用到Tag时,容易造成Tag使用的混乱,Tag是如此重要的一个属性,应该要好好考虑下如何有效的使用T ...

  7. hotspot虚拟机的调试

    3这篇文章,怎么说呢.是踩了很多坑得出来了,也是在自己快要崩溃的时候得出来了的. 连续踩了差不多10来个小时的坑,还好是出来了. 这篇文章是调试虚拟机的,其实网上也能找到一些文章,但是每个人的环境不一 ...

  8. BST 解析 (二)height and deletion

    前面一章介绍了BST的结构和一些简单的基本功能,例如:insert,findMin,nextLarger等等.这一节主要讲解一些BST的delete node操作还有BST的height的分析以及一些 ...

  9. win7及以上系统打开chm空白或显示"无法打开"的2个解决方案

    1:在文件上右键单击属性.选择解锁. 2:确保文件路径中没有特殊字符.比如"#".如果路径中存在特殊字符,chm文件可能找不到路径而无法显示.如 d:\资料\c#\***.chm. ...

  10. openstack windows 2008镜像 制作

    openstack windows 2008镜像 制作 openstack centos6 centos7 kvm 镜像制作 http://www.cnblogs.com/elvi/p/7922421 ...