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 ...
随机推荐
- maven入门(1-4)使用eclipse构建maven项目
1. 安装m2eclipse插件 要用Eclipse构建Maven项目,我们需要先安装meeclipse插件 点击eclipse菜单栏Help->Eclipse Marketplac ...
- Linux命令(持续更新中)
命令名 用法 安装上传下载 yum install lrzsz rz上传文件,sz下载文件 压缩 解压文件 tar -zxvf 文件名 压缩文件 tar -zcvf 文件名 删除非空目录: rm ...
- django关闭调试信息,打开内置错误视图
1 内置错误视图 Django内置处理HTTP错误的视图,主要错误及视图包括: 404错误:page not found视图 500错误:server error视图 400错误:bad reques ...
- Lintcode373 Partition Array by Odd and Even solution 题解
[题目描述] Partition an integers array into odd number first and even number second. 分割一个整数数组,使得奇数在前偶数在后 ...
- python Http协议
Http协议 一 HTTP概述 HTTP(hypertext transport protocol),即超文本传输协议.这个协议详细规定了浏览器和万维网服务器之间互相通信的规则. HTTP就是一个通信 ...
- SecureCRT安装
第一步:下载SecureCRT&SecureCRT激活工具 首先下载SecureCRT安装包和SecureCRT激活工具,SecureCRT&SecureCRT激活工具下载地址:链接: ...
- github git 在GitHub上创建项目并将本地项目push到网站上
众所周知,git是与svn类似的版本控制系统,git的去中心化.分布式等的优点,在不久将来用户量大有可能超过svn, 常见的代码托管网站有GitHub,coding.net, gitee.com 码云 ...
- Java高级篇(二)——网络通信
网络编程是每个开发人员工具相中的核心部分,我们在学习了诸多Java的知识后,也将步入几个大的方向,Java网络编程就是其中之一. 如今强调网络的程序不比涉及网络的更多.除了经典的应用程序,如电子邮件. ...
- mongol学习
启动mongodb 服务器: 进入mongodb文件夹:cd ~/mongodb 第一次先要创建set与log文件夹. mkdir set; mkdir log; 并创建 ...
- MariaDB表表达式(2):CTE
本文目录: 1.非递归CTE 2.递归CTE 2.1 语法 2.2 递归CTE示例(1) 2.3 递归CTE示例(2) 2.4 递归CTE示例(3) 公用表表达式(Common Table Expre ...