39. Combination Sum(medium, backtrack 的经典应用, 重要)
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times.
Note:
- All numbers (including the target) will be positive integers.
- The solution set must not contain duplicate combinations.
For example, given candidate set [2, 3, 6, 7] and target 7,
A solution set is:
[
[7],
[2, 2, 3]
]
本题是 backtrack(回溯法) 的经典应用.通过本题,仔细观察 backtrack 基本框架,并感受其应用.
另外, subset 的题目也用 backtrack 方法.
人家想法,自个代码:
- cpp 副产品:
v.pop_back();
弹出队尾元素. - 要注意的是: 下面代码中 backtrack 方法中的 res, temp, A, 尤其是 temp, 他们都分别指向对应的一个对象,无论 backtrack 方法被嵌套地调用多少次!
// backtrace method
vector<vector<int>> combinationSum(vector<int>& A, int ta) {
sort(A.begin(), A.end());
vector < vector<int> > res;
vector<int> temp;
backtrack(res, temp, A, ta, 0);
return res;
}
void backtrack(vector<vector<int> >& res, vector<int>& temp, vector<int>& A,
int remain, int start) {
if (remain < 0)
return;
else if (remain == 0) {
res.push_back(temp);
return;
} else {
for (int i = start; i < A.size(); i++) {
temp.push_back(A[i]);
// not i + 1, because A[i] might be reused.
backtrack(res, temp, A, remain - A[i], i);
temp.pop_back();
}
return;
}
}
39. Combination Sum(medium, backtrack 的经典应用, 重要)的更多相关文章
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- [Leetcode][Python]39: Combination Sum
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 39: Combination Sumhttps://oj.leetcode. ...
- LeetCode题解39.Combination Sum
39. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- 39. Combination Sum - LeetCode
Question 39. Combination Sum Solution 分析:以candidates = [2,3,5], target=8来分析这个问题的实现,反向思考,用target 8减2, ...
- 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV
▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...
- [LeetCode] 39. Combination Sum 组合之和
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- 【LeetCode】39. Combination Sum (2 solutions)
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- 39. Combination Sum (Back-Track)
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
随机推荐
- istio入门(01)istio是什么?
- Scala入门(1)Linux下Scala(2.12.1)安装
Scala入门(1)Linux下Scala(2.12.1)安装 一.文件准备 1.1 文件名称 scala-2.12.1.tgz 1.2 下载地址 http://www.scala-lang.org/ ...
- linux ubunt 安装软件的前期准备——更新源的更换
如果是高手,请翻到页面最下方,更换更新源的总结,直接操作即可 可能会优点啰嗦,但是认真看,一定能解决问题~~希望对大家有帮助~ 最近在熟悉linux环境,自己安装了一个ubuntu虚拟机. 很多朋友问 ...
- 浅谈 DML、DDL、DCL的区别
一.DML DML(data manipulation language)数据操纵语言: 就是我们最经常用到的 SELECT.UPDATE.INSERT.DELETE. 主要用来对数据库的数据进行一些 ...
- windows10无法启动承载网络
每个都试一下
- geotrellis使用(三十七)COG 基础介绍
前言 Geotrellis 已经迭代到了 2.0 版本(截止写作此文时为 2.0.0-SNAPSHOT 版),2.0 版多了很多新的特性,其中最重要的应该就是 COG,COG 是什么鬼?刚看到时我也是 ...
- BeautifulSoup重点复习
html = """ <html><head><title>The Dormouse's story</title>< ...
- Python面向对象——内置对象的功能扩展
1.扩展Python内置类 Python的数据类型 列表(list).字典(dict).集合(set).文件(file).字符串(str),这些都是对象 扩展list的功能,详解如图: 我们给列表添加 ...
- 使用开源数据库客户端DBeaver连接DB2数据库
下载安装 首先进入 官网 选择对应的版本进行安装. 下载下来后,一直惦记next即可完成安装(期间包括选择文件安装路径等操作,可按需修改). 连接db2 打开DBeaver,新建连接-->DBe ...
- java模式之模板模式——抽象类
模板设计模式(Template ) abstract class Action{ // 定义一个行为类 public static final String WORK = "work&quo ...