40. Combination Sum II(midum, backtrack, 重要)
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 the target) will be positive integers.
- 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]
]
这题用到 backtrack 方法, 需去重.
e.g.
A = [1 1 2 5 6 7 10], target = 8
正确输出应该是:
[[1,1,6],[1,2,5],[1,7],[2,6]]
难点在去重条件:
* 我写的,错误: `if(i >= 1 && A[i] == A[i - 1]) continue;`
- 错误输出: `[[1,2,5],[1,7],[2,6]]`
* 人家写的,正确: `if ((i >= 1) && (A[i] == A[i - 1]) && (i > start)) continue;`
差别就是 i > start 条件,挺不容易的想出来的.
自己想法,自个代码(被人家修正^^):
// backtrack
// A = [1 1 2 5 6 7 10]
vector<vector<int>> combinationSum2(vector<int>& A, int ta) {
sort(A.begin(), A.end());
vector < vector<int> > res;
vector<int> temp;
backtrack(A, res, temp, ta, 0);
return res;
}
void backtrack(vector<int>& A, vector<vector<int> >& res, vector<int> temp,
int remain, int start) {
if (remain < 0)
return;
else if (remain == 0)
res.push_back(temp);
else {
for (int i = start; i < A.size(); i++) {
// not correct: if(A[i] == A[i - 1] && i >= 1) continue;
// (i > start) is hard think, but important.
if ((i >= 1) && (A[i] == A[i - 1]) && (i > start))
continue; // check duplicate combination
temp.push_back(A[i]);
backtrack(A, res, temp, remain - A[i], i + 1); // i + 1, next element
temp.pop_back();
}
}
}
40. Combination Sum II(midum, backtrack, 重要)的更多相关文章
- [Leetcode][Python]40: Combination Sum II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- 【LeetCode】40. Combination Sum II (2 solutions)
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- [LeetCode] 40. Combination Sum II 组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- 40. Combination Sum II (JAVA)
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- LeetCode OJ 40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【一天一道LeetCode】#40. Combination Sum II
一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...
随机推荐
- VS2013 工程属性配置
1. 配置属性设置 设置工程编译输出目录 2. 设置第三方库的头文件的位置 3.设置第三方库(动态库或者静态库链接的搜寻的目录) 4.设置链接的第三方库的名称 注: 第三方库的链接可以通过配置文件来实 ...
- WebBench的安装与使用
webbench最多可以模拟3万个并发连接去测试网站的负载能力. 一.编译安装 1.上传压缩包到虚机里,rz webbench-1.5.tar.gz 2.解压 tar zxvf webbench-1. ...
- python、java实现二叉树,细说二叉树添加节点、深度优先(先序、中序、后续)遍历 、广度优先 遍历算法
数据结构可以说是编程的内功心法,掌握好数据结构真的非常重要.目前基本上流行的数据结构都是c和c++版本的,我最近在学习python,尝试着用python实现了二叉树的基本操作.写下一篇博文,总结一下, ...
- python基础——面向对象进阶下
python基础--面向对象进阶下 1 __setitem__,__getitem,__delitem__ 把对象操作属性模拟成字典的格式 想对比__getattr__(), __setattr__( ...
- 给定了经纬度的一张my_latlng表,和一个my_grid表,怎么实现my_latlng表回mygrid中的id?
场景: 假设我们拥有一个拥有了一系列经纬度的表my_latlng(lat string,lng string)表,还有一张给定的栅格表my_grid(gridid bigint,centerlng d ...
- 【DataMagic】如何在万亿级别规模的数据量上使用Spark
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文首发在云+社区,未经许可,不得转载. 作者:张国鹏 | 腾讯 运营开发工程师 一.前言 Spark作为大数据计算引擎,凭借其快速.稳定. ...
- 初探Javascript之DOM
DOM对象(文档对象模型) HTML DOM 是 W3C 标准(是 HTML 文档对象模型的英文缩写,Document Object Model for HTML).HTML DOM 定义了用于 HT ...
- php array_multisort函数实现按某一字段对二维数组进行排序
在工作中碰到一个页面如表格似的展示多条数据,要求根据其中的修改时间对数据进行排序, 数据格式类似于 $a = array( 0=>array( editTime=>'' addTime=& ...
- 规约模式(Specification Pattern)
一.引言 最近在看一个项目的源码时(DDD),对里面的一些设计思想和设计思路有了一些疑问.当看到(Repository层)中使用了 spec.SatisfiedBy() 时,感觉有点懵.于是在项目中搜 ...
- [BZOJ]2589: Spoj 10707 Count on a tree II
Time Limit: 20 Sec Memory Limit: 400 MB Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v),你需要回答u xor last ...