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 ...
随机推荐
- 哪些异常是RuntimeException?Sql异常属于RuntimeException吗?Spring下SQL异常事务回滚
一,为什么框架中根本没有对Exception的一般子类进行回滚配置,异常发生时,事务都进行了回滚 ,说好的只会对RuntimeException(Unchecked 非受检异常)回滚呢? 此时,我们就 ...
- poj 1639 Picnic Planning 度限制mst
https://vjudge.net/problem/POJ-1639 题意: 有一群人,他们要去某一个地方,每个车可以装无数个人,给出了n条路,包含的信息有路连接的地方,以及路的长度,路是双向的,但 ...
- 02、NetCore2.0优化之Nuget包
02.NetCore2.0优化之Nuget包 在NetCore2.0中的包是如何管理的?如何存储的?微软做了哪些优化工作? -------------------------------------- ...
- 06、NetCore2.0依赖注入(DI)之整合Autofac
06.NetCore2.0依赖注入(DI)之整合Autofac 除了使用NetCore2.0系统的依赖注入(DI)框架外,我们还可以使用其他成熟的DI框架,如Autofac.Unity等.只要他们支持 ...
- DbContext(String)+SqlQuery一起使用
DbContext(String) 可以将给定字符串用作将连接到的数据库的名称或连接字符串来构造一个新的上下文实例. Database.SqlQuery 方法 (Type, String, Objec ...
- bootstrap——bootstrap-table(2)
先看问题现象: 问题描述: 点击"管理子账号"按钮,预期是按照传递的参数更新列表数据,但是最后才发现这个列表根本不会刷新,只会继承前一次的数据,意思也就是不会去请求第二次. 解决方 ...
- [LeetCode] Exclusive Time of Functions 函数的独家时间
Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...
- jenkins实战(一):war安装及插件安装
一:整体介绍 以下摘自维基百科: Jenkins是一个用Java编写的开源的持续集成工具.在与Oracle发生争执后,项目从Hudson项目复刻. Jenkins提供了软件开发的持续集成服务.它运行在 ...
- [SDOI2010]地精部落
题目描述 传说很久以前,大地上居住着一种神秘的生物:地精. 地精喜欢住在连绵不绝的山脉中.具体地说,一座长度为N的山脉H可分为从左到右的N段,每段有一个[b][u]独一无二[/u][/b]的高度Hi, ...
- 【BZOJ3224】【tyvj1728】普通平衡树
最近开始学习平衡树,在学长的强烈推荐下学习了AVL.红黑树.splay(以上我都还没学)treap. 首先讲一下个人对treap(树堆)的理解. treap,顾名思义,就是tree+heap,首先因为 ...