216. Combination Sum III(medium, backtrack, 本类问题做的最快的一次)
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.
Example 1:
Input: k = 3, n = 7
Output:
[[1,2,4]]
Example 2:
Input: k = 3, n = 9
Output:
[[1,2,6], [1,3,5], [2,3,4]]
关于 backtrack problem 做的最快的一次.
定义了2个东西:
- start
- remain
自己想法,自己代码:
vector<vector<int>> combinationSum3(int k, int n) {
vector<int> A = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
vector<vector<int> > res;
vector<int> temp;
backtrack(res, temp, A, k, 0, n);
return res;
}
void backtrack(vector<vector<int> >& res, vector<int>& temp, vector<int>& A,
int k, int start, int remain) {
if (temp.size() == k && remain == 0) {
res.push_back(temp);
return;
}
for (int i = start; i < A.size(); i++) {
temp.push_back(A[i]);
backtrack(res, temp, A, k, ++start, remain - A[i]);
temp.pop_back();
}
}
216. Combination Sum III(medium, backtrack, 本类问题做的最快的一次)的更多相关文章
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- [LeetCode] 216. Combination Sum III 组合之和 III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- 【LeetCode】216. Combination Sum III
Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...
- LC 216. Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- 【刷题-LeetCode】216. Combination Sum III
Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...
- 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV
▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Leetcode 216. Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- LeetCode 216. Combination Sum III (组合的和之三)
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
随机推荐
- kubernetes入门(10)kubernetes单机安装后 - helloworld
前言 查看端口是否被监听了 ::netstat -tlp |grep 31002 我是用的yum install etcd kubernetes docker vim, 这样装的是1.5.2,不是最新 ...
- 【Java】0X003 面向对象
一. 什么是面向对象 都说Java是一门面向对象的语言,但什么对象?什么又是面向对象?以下都是我学到的知识和一点自己的理解. 对象是指包含属性和行为的主体. 比如,人有性别.血型.单眼皮或双眼皮等的特 ...
- VS2013 堆栈溢出调查(0xC00000FD: Stack overflow)
在调试一个代码时,执行过程中会出现如下错误(0xC00000FD: Stack overflow). 很明显是堆栈溢出了. 网上很多方法,都是通过修改设置工程配置,把堆栈调大一些,如下图. 但是堆栈到 ...
- 基于UDP协议的控制台聊天程序(c++版)
本博客由Rcchio原创,转载请告知作者 ------------------------------------------------------------------------------- ...
- CSS优先级和定位
overflow属性 hidden scroll auto hidden 超出隐藏 scroll 滚动条 Auto 自动 display属性 block inline inline-block non ...
- python基础——面向对象进阶下
python基础--面向对象进阶下 1 __setitem__,__getitem,__delitem__ 把对象操作属性模拟成字典的格式 想对比__getattr__(), __setattr__( ...
- windows10无法启动承载网络
每个都试一下
- jQuery获取浏览器参数
当我们需要获取浏览器参数是,我们可以使用jQuery进行获取,具体方法如下: 例如获取参数的地址:http://www.test.com?a="111"&b="2 ...
- JavaScript 中常见的内存泄露陷阱(摘)
内存泄露是每个开发者最终都不得不面对的问题.即便使用自动内存管理的语言,你还是会碰到一些内存泄漏的情况.内存泄露会导致一系列问题,比如:运行缓慢,崩溃,高延迟,甚至一些与其他应用相关的问题. 什么是内 ...
- laypage分页控件使用方法
laypage是一款非常简单易用的分页控件,由于最近项目中使用到了,简单记录一下使用方法 1.引入laypage所需的js和css文件 <link href="js/laypage/1 ...