给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的每个数字在每个组合中只能使用一次。

说明:

  • 所有数字(包括目标数)都是正整数。
  • 解集不能包含重复的组合。

示例 1:

输入: candidates = [10,1,2,7,6,1,5], target = 8, 所求解集为: [ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ]

示例 2:

输入: candidates = [2,5,2,1,2], target = 5, 所求解集为: [   [1,2,2],   [5] ]

相比组合种数这道题加了个新加了去重操作,即递归了一个数后,回溯到此层时,把数组之后和该数相同的给过滤掉,防止出现重复的答案。

bool cmp1(int x, int y)
{
return x < y;
} class Solution {
public:
vector<vector<int> > res;
vector<vector<int>> combinationSum2(vector<int>& candidates, int target)
{
int len = candidates.size();
sort(candidates.begin(), candidates.end(), cmp1);
vector<int> temp;
DFS(candidates, target, temp, 0);
return res;
} void DFS(vector<int> candidates, int val, vector<int> &v, int cnt)
{
if(val == 0)
{
res.push_back(v);
return;
}
for(int i = cnt; i < candidates.size(); i++)
{
if(candidates[i] <= val)
{
v.push_back(candidates[i]);
DFS(candidates, val - candidates[i], v, i + 1);
v.pop_back();
for(; i < candidates.size() - 1; i++)
{
if(candidates[i] == candidates[i + 1])
continue;
else
break;
}
}
}
}
};

Leetcode40. Combination Sum组合总数2 II的更多相关文章

  1. LeetCode40 Combination Sum II

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

  2. [LeetCode] Combination Sum 组合之和

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

  3. [LeetCode] 39. Combination Sum 组合之和

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

  4. [leetcode]39. Combination Sum组合之和

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

  5. LeetCode OJ:Combination Sum (组合之和)

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

  6. 039 Combination Sum 组合总和

    给一组候选数字(C)(没有重复)并给一个目标数字(T),找出 C 中所有唯一的组合使得它们的和为 T.可以从 C 无限次数中选择相同的数字.说明:    所有数字(包括目标)都是正整数.    解集合 ...

  7. 【LeetCode】Combination Sum(组合总和)

    这道题是LeetCode里的第39道题. 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组 ...

  8. Leetcode39.Combination Sum组合总和

    给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选 ...

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

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

随机推荐

  1. 70 二叉树的层次遍历 II

    原题网址:http://www.lintcode.com/zh-cn/problem/binary-tree-level-order-traversal-ii/ 给出一棵二叉树,返回其节点值从底向上的 ...

  2. 使用 Vue.js 和 Chart.js 制作绚丽多彩的图表

    本文作者:Jakub Juszczak 编译:胡子大哈 翻译原文:http://huziketang.com/blog/posts/detail?postId=58e5e0e1a58c240ae35b ...

  3. Neo4j删除节点和关系、彻底删除节点标签名

    https://www.jianshu.com/p/59bd829de0de 总结提前: [1]先删关系,再删节点 [2]当记不得关系名时,type(r)可以查到关系名 [3]彻底删除节点标签名,需要 ...

  4. C++面向对象高级编程(下)第一周-Geekband

    勿在浮沙筑高台 革命尚未成功,同志仍需努力 <h1> Conversion Function</h1> class Fraction { public: Fraction(in ...

  5. linux-c getopt()参数处理函数

    转自:https://www.cnblogs.com/qingergege/p/5914218.html 最近在弄Linux C编程,本科的时候没好好学啊,希望学弟学妹们引以为鉴. 好了,虽然啰嗦了点 ...

  6. 同一服务器不同域名session共享

    Tomcat下,不同的二级域名之间或根域与子域之间,Session默认是不共享的,因为Cookie名称为JSESSIONID的Cookie根域是默认是没设置 的,访问不同的二级域名,其Cookie就重 ...

  7. python内置类型详细解释

    文章编写借鉴于内置类型 - Python 3.7.3 文档,主要用于自己学习和记录 python主要内置类型包括数字.序列.映射.类.实例和异常 有些多项集类是可变的.它们用于添加.移除或重排其成员的 ...

  8. 系统负载load

    一.查看系统负荷 如果你的电脑很慢,你或许想查看一下,它的工作量是否太大了. 在Linux系统中,我们一般使用uptime命令查看(w命令和top命令也行).(另外,它们在苹果公司的Mac电脑上也适用 ...

  9. Linux 修改环境变量,重定向

    1.Linux下更改(当前用户)环境变量:在terminal下输入vim ~/.profile进入后,在最后一行添加PATH="-----------------:$PATH",添 ...

  10. Python开发之MySQL安装

    MySQL下载安装后再安装破解版本的Navicat图形化数据库工具即可.   安装python后.再进行如下操作(也可以安装好虚拟环境virtualenv 或者增强工具pip install virt ...