leetcode第39题--Combination Sum II
题目:
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的更多相关文章
- LeetCode(40) Combination Sum II
题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations ...
- leetcode第38题--Combination Sum
题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C ...
- LeetCode(39) Combination Sum
题目 Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C w ...
- LeetCode:40. Combination Sum II(Medium)
1. 原题链接 https://leetcode.com/problems/combination-sum-ii/description/ 2. 题目要求 给定一个整型数组candidates[ ]和 ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)
Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- 乘风破浪:LeetCode真题_040_Combination Sum II
乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...
- 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) ...
随机推荐
- metaq spring
spring metaq spring bean 配置 <bean id="sessionFactory" class="com.taobao.metamorpho ...
- Android IPC通信和AIDL技术应用
首先我们了解一下 IPC和AIDL IPC:进程间通信 AIDL:Android Interface Definition Language,即Android接口定义语言. 为什么使用: Androi ...
- 开源项目:底部动作条(BottomSheet)
底部动作条(BottomSheet)是一个从屏幕底部边缘向上滑出的一个面板,给用户呈现一组功能选项.底部动作条封装了一组简单.清晰.无需额外说明的操作.底部动作条(如下图)可以是列表样式的,也可以是宫 ...
- DevExpress XtraReports 入门一 创建 Hello World 报表
原文:DevExpress XtraReports 入门一 创建 Hello World 报表 本文只是为了帮助初次接触或是需要DevExpress XtraReports报表的人群使用的,为了帮助更 ...
- NYNU_省赛选拔题(5)
题目描述 P 的一家要出去旅游,买了当地的地图,发现各地分别由各个景点,若 P 想使家人分队去景点,尽快到达各个景点(必须所有景点),并且最终所有家人都到达 M 所在的景点. 你用程序告诉 P 最 ...
- 国外android开源站点
http://android-arsenal.com/
- hdu More is better
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1856 题意:王老师要找一些男生帮助他完成一项工程.要求最后挑选出的男生之间都是朋友关系,可以说直接的, ...
- Cocos2d-x学习笔记(两)Cocos2d-x总体框架
原创文章.转载请注明出处:http://blog.csdn.net/sfh366958228/article/details/38680123 前言 上一节我们简单分析了HelloWorldproje ...
- 【高德地图API】从零开始学高德JS API(七)——定位方式大揭秘
原文:[高德地图API]从零开始学高德JS API(七)——定位方式大揭秘 摘要:关于定位,分为GPS定位和网络定位2种.GPS定位,精度较高,可达到10米,但室内不可用,且超级费电.网络定位,分为w ...
- CSS3 制作向左、向右及关闭图标的效果
先看一下效果 1.鼠标移入前的效果 2.鼠标移入的效果 3.制作步骤如下: 轮廓的CSS,就是利用圆角和宽度高度,制作出一个圆: <style> /*显示方式为 inline-block* ...