题目:

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. NHibernate框架魅力美

    Nhibernate属于ORM框架之中的一个,在了解NHibernate之前我们先来了解什么是ORM? ORM框架是为了将类对象和关系建立映射.事实上说白了,就是通过一个 Mapping将我们的实体类 ...

  2. asp.net使用post方式action到另一个页面,在另一个页面接受form表单的值!(报错,已解决!)

    原文:asp.net使用post方式action到另一个页面,在另一个页面接受form表单的值!(报错,已解决!) 我想用post的方式把一个页面表单的值,传到另一个页面.当我点击Default.as ...

  3. 设计模式16:迭代模式(Iterator)

    迭代模式: 它提供了一种方法没有对象的顺序访问聚合对象的暴漏底层的细节. Provide a way to access the elements of an aggregate object seq ...

  4. CSS hack方式

    史上最全的CSS hack方式一览   做前端多年,虽然不是经常需要hack,但是我们经常会遇到各浏览器表现不一致的情况.基于此,某些情况我们会极不情愿的使用这个不太友好的方式来达到大家要求的页面表现 ...

  5. unix您不能使用crontab设置运营计划

    unix您不能使用crontab设置运营计划 在系统中进行crontab例如,设置在下列现象时有发生: 解决方法: 编辑cron文件内容: #EDITOR=vi  #export EDITOR     ...

  6. VS2008让自己掌控的定义编译项目后,自己主动添加到工具箱

    在VS2008中,假设在项目里写了一个用户控件.编译后这个控件是不会自己主动出现到工具箱的.按例如以下设置就能够解决问题 工具=>选项=>Windows窗口设计器=>常规=>A ...

  7. linux 安装jdk-7u45-linux-x64.tar.gz

    1.官网下载jdk-7u45-linux-x64.tar.gz 载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-dow ...

  8. HDU 3729 I&#39;m Telling the Truth(二部图最大匹配+结果输出)

    职务地址:HDU 3729 二分图最大匹配+按字典序输出结果. 仅仅要从数字大的開始匹配就能够保证字典序最大了.群里有人问. . 就顺手写了这题. . 代码例如以下: #include <ios ...

  9. 创建和分析excel文件

    jxl.jar:下载地址:http://download.csdn.net/detail/xuxu198899223/7717737 package excel; public class BookV ...

  10. CodeIgniter连接数据库及快速入门

    原文:CodeIgniter连接数据库及快速入门 一.数据库配置 CodeIgniter 有一个配置文件让你存放数据库连接值(username:用户名,password:密码,database nam ...