题目:

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.
  • Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
  • 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]

国家德比毕。记录此题。

和上题的意思差不多,不同在于candidates的每个数在一个组合中只能用一次。

一开始以为很简单,只要把上一题的back函数传入的下标加1就可以了。这还不够,还要避免一些重复的case。例如[1,1]的时候,目标是1的时候,如何只输出一个答案。所以我们应该注意到,在每一层中,如果之前一个数和当前的数相同,并且之前那个数是已经进入tmp尝试过的,那么当前的就应该跳过,因为这个情况之前已经判断过了,再判断就是重复了。一开始我用candidates[i-1]和candidates[i]相比是否相同,发现还是不行。因为candidades[i-1]不一定在之前考虑过。很简单,就是在[1,1]的case中目标是2的话,如果用candidates的i-1和i的相比的话就会出现错误,因为在写成树结构的时候,第二层中的第一个1是没有考虑的,所以第二个1不会重复(简单画一个二叉树就知道了)。

class Solution {
public:
void back39(vector<vector<int> > &ans, vector<int> &candidates, vector<int> &tmp, int target, int cur)
{
if (0 == target)
{
ans.push_back(tmp);
return;
}
int rp = -1;
for (int i = cur; i < candidates.size(); ++i)
{
//需要判断当前的candidate是不是和上一次加进去并且删除了的一样,如果一样的话因为是同一层所以跳过
//本来想用candidates[i-1]和candidates[i]比较,但并不是所有的前一个都被列入到tmp中,只有列入到tmp中的才算重复
//例如当输入为[1,1] 目标为2的时候用candidates[i-1]和candidates[i]就不行
if (rp == candidates[i])
continue;
if (candidates[i] <= target)
{
rp = candidates[i]; // 记录,为了和下次进行比较,如果一样则continue
tmp.push_back(candidates[i]);
back39(ans, candidates, tmp, target - candidates[i], i + 1);
tmp.pop_back();
}
if (candidates[i] > target)
{
return;
}
}
}
vector<vector<int> > combinationSum2(vector<int> &num, int target)
{
std::sort(num.begin(), num.end());
vector<vector<int> > ans;
vector<int> tmp;
back38(ans, num, tmp, target, 0);
return ans;
}
};

leetcode第39题--Combination Sum II的更多相关文章

  1. LeetCode(40) Combination Sum II

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

  2. leetcode第38题--Combination Sum

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

  3. LeetCode(39) Combination Sum

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

  4. LeetCode:40. Combination Sum II(Medium)

    1. 原题链接 https://leetcode.com/problems/combination-sum-ii/description/ 2. 题目要求 给定一个整型数组candidates[ ]和 ...

  5. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

  6. Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)

    Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...

  7. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  8. 乘风破浪:LeetCode真题_040_Combination Sum II

    乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...

  9. LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings

    1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...

随机推荐

  1. K60 启动过程分析

    很高兴老师借给我一K60的开发板,趁着暑假好好鼓捣鼓捣! 有了上图的过程分析我想心里大概有个低了吧! 以下看代码: /* CodeWarrior ARM Runtime Support Library ...

  2. Windows下Jekyll安装

    一直用Mac,换了新公司使用的电脑是windows,网上粗略的看了一下Jekyll的安装.简略的实现了一遍 首先安装Ruby Ruby安装文件下载地址 下载对应版本,我的电脑是64位的下载64位的版本 ...

  3. The maximum string content length quota (8192) has been exceeded while reading XML data

    原文:The maximum string content length quota (8192) has been exceeded while reading XML data 问题场景:在我们W ...

  4. angularjs从零开始(一)

    简介   AngularJS是为了克服HTML在构建应用上的不足而设计的.HTML是一门很好的为静态文本展示设计的声明式语言,但要构建WEB应用的话它就显得乏力了.所以我做了一些工作(你也可以觉得是小 ...

  5. ServiceStack.Redis——Redis于.net向上client解

    ServiceStack.Redis--Redis于.net向上client解 源代码和使用: https://github.com/ServiceStack/ServiceStack.Redis 样 ...

  6. MAC OSX在视图port哪个程序占用,杀死进程的方法

    sudo lsof -i :9000 COMMAND   PID    USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME java    6 ...

  7. 完全背包 (DP)

    输入: n=3 (w,v)={(3,4),(4,5),(2,3)} W=7 输出: 10(0号物品选1个,2号物品选2个) 和01背包的区别是物品可以任意选择. 令dp[i+1][j]=从前i种物品中 ...

  8. Saving HDU (贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2111 好久不刷题,拿到水题切了切,,,,,题意刚开始都没有理解,,,,真是弱了,,,, 简单贪心,,, ...

  9. crm使用soap删除实体

    //C# 代码: //DeleteEntityRequest request = new DeleteEntityRequest(); //request.LogicalName = "ne ...

  10. 教你Ant安装和配置

    Ant它是基于Java施工工具,它的主要作用是产生能够运行Java计划,把握Ant一些功能可以使项目更专业. 忙乱,可以在这里下载2014年8最近一个月Ant http://download.csdn ...