题目:

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. 使用cocoapods install友盟时报错Error installing UMengAnalytics

    报错: [!] /usr/bin/unzip /Users/soindy/Documents/SmartThermo/ios/SmartThermo/Pods/UMengAnalytics/file. ...

  2. C语言API编写窗口界面和button

            近期有个同学的程序须要用对话框的方式实现,但前面都是通过黑框形式完毕的,老师突然让添加一个界面,本来准备採用MFC完毕的,但后来一想,该程序核心东西是体如今它的算法上,控制台的程序并不 ...

  3. HDOJ 3480 Division

    斜率优化DP. ... 对数组排序后.dp[i][j]表示对前j个物品分i段的最少代价,dp[i][j]= min{ dp[i-1][k]+(a[k+1]-a[j])^2 }复杂度m*n^2     ...

  4. Extjs grid column里添加button等html标签,并增加点击事件

    Extjs里有个actioncolumn,但actioncolumn只能添加一系列button,不能既有字又有button 如何能在column里增加html标签,并给button添加事件呢? 1. ...

  5. zoj-3792-Romantic Value-最小割+数值转化

    假设不须要求边的个数的话,就是一个裸的最小割问题. 求边的个数就用边的权值记录一下. #include <stdio.h> #include <iostream> #inclu ...

  6. MyEclipse中将项目导出jar包,以及转化成EXE文件

    1.对于项目如何导出jar文件,和生成exe,解答目录如下: 首先生成jar文件,单击项目名称CF-users(这是我的协同过滤项目文件名称)右击--->Export如下图: 单击下一步 Sel ...

  7. 通过.NET实现后台自动发送Email功能的代码示例

    原文:通过.NET实现后台自动发送Email功能的代码示例 通过.NET实现后台自动发送邮件功能的代码,可以将一些基础信息放到web.config文件中进行保存: Web.config文件信息段: & ...

  8. 类(class)能不能自己继承自己(转)

    类(class)能不能自己继承自己不行,继承关系会出现环. 假设类A继承类A.那么要新建一个类A的对象,就必须先建立一个类A父类的对象.这是一个递归的过程,而且没有终止条件.会死循环的. 从编译的角度 ...

  9. Atitit.软体guibuttonand面板---通信子系统(范围)-- github 采用....

    Atitit.软体guibuttonand面板---通讯子系统(区)-- github 的使用.... 1. 1.注冊账户以及创建仓库 1 2. 二.在GitHub中创建项目(create a new ...

  10. 如何使用 RMAN 异构恢复一些表空间

    在oracle 在日常维护的数据库中难免会遇到误删数据和使用(drop.delete. truncate)当我们使用常规手段(flashback query .flashback drop)当数据不能 ...