给定一个数组 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. Sql Server 中查询存储过程的修改时间

    1.按最近修改排序所有存储过程 SELECT [name], [create_date], [modify_date] FROM [sys].[objects] WHERE [type] = 'P' ...

  2. Class绑定v-bind:class

    <!DOCTYPE html> <html lang="zh"> <head> <title></title> < ...

  3. 模板——二分图匹配KM

    具体方法就不介绍了,详见 https://blog.csdn.net/sixdaycoder/article/details/47720471 主要讲一些注意点: 1:不直接将未匹配的y减小是因为要保 ...

  4. 博弈论 | 暑期集训Day2学习总结

    今天的知识点为博弈论. 相比于昨天完全陌生难懂的概念,今天接触到的东西应该算是非常容易理解了,一下子又对ACM的学习重拾信心.毕竟game作为主题也吸引眼球,每种博弈背景下引入的游戏介绍也十分有趣.主 ...

  5. 服务器IP配置功能实现小结

    1. 服务器网卡配置文件 /etc/sysconfig/network/ifcfg-***(eth0) linux-f1s9:/etc/sysconfig/network # cat ifcfg-et ...

  6. eclipse memory analyzer对系统内存溢出堆文件解析0(转)

    前言 在平时工作过程中,有时会遇到OutOfMemoryError,我们知道遇到Error一般表明程序存在着严重问题,可能是灾难性的.所以找出是什么原因造成OutOfMemoryError非常重要.现 ...

  7. 手机端META详细解释

    一.天猫 <title>天猫触屏版</title> <meta content="text/html; charset=utf-8" http-equ ...

  8. elasticsearch 中文API 基于查询的删除(九)

    基于查询的删除API 基于查询的删除API允许开发者基于查询删除一个或者多个索引.一个或者多个类型.下面是一个例子. import static org.elasticsearch.index.que ...

  9. HBase 概念视图

  10. Python :route的用法以及Http方法

    现代 Web 应用的 URL 十分优雅,易于人们辨识记忆,这一点对于那些面向使用低速网络连接移动设备访问的应用特别有用.如果可以不访问索引页,而是直接访问想要的那个页面,他们多半会笑逐颜开而再度光顾. ...