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 ...
随机推荐
- kafka之zookeeper 节点
1.zookeeper 节点 kafka 在 zookeeper 中的存储结构如下图所示:
- Python3安装Requests
安装Requests费了1天的时间,囧.终于还是在官网找到解决方法,可以参考这个http://docs.python-requests.org/en/latest/user/install/#inst ...
- TR069网管协议应用在Android系统开发的前言
随着智能平台的终端设备不断发展,迫切需要我们解决这些终端的管理问题,而现有的终端统一管理平台已经成熟,主要是基于tr069协议网管平台,比如华为的itms等终端管理平台.所以,这篇文章就是为了实现一种 ...
- Spark测试代码
测试代码: import org.apache.spark.{SparkConf, SparkContext} import org.apache.spark.sql.hive.HiveContext ...
- Java:逐行读、写文件、文件目录过滤的用法
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.I ...
- WebGL文字渲染的那些问题
THREE.js开发的应用运行在iphone5下发现有些时候会崩溃,跟了几天发现是因为Sprite太多频繁更新纹理占用显存导致的.通常解决纹理频繁更新问题就要用到one draw all方法,放到纹理 ...
- laravel5.5 使用alipay SDK报错Cannot redeclare Encrypt() (previously declared in ../vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:448)
错误现象: 在laravel5.5 中,使用alipaySDK 报错: Cannot redeclare Encrypt() (previously declared in ../vendor/lar ...
- 031718-js变量、数据类型、运算符
1.关键字.标识符.变量(是一个名称,最好用字母开头,对大小写敏感).常量 (是有数据类型的一个值) 变量: ①定义并赋值 ②使用 2.数据类型:数字 字符串 布尔 null undefi ...
- [LeetCode] Freedom Trail 自由之路
In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal ...
- 机器学习技法:12 Neural Network
Roadmap Motivation Neural Network Hypothesis Neural Network Learning Optimization and Regularization ...