【LeetCode】216. Combination Sum III
Combination Sum III
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.
Ensure that numbers within the set are sorted in ascending order.
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]]
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
参考Combination Sum II,把去重条件去掉(1-9本身就不包含重复元素),仅返回包含k个元素的vector
class Solution {
public:
vector<vector<int>> combinationSum3(int k, int n) {
vector<int> num();
for(int i = ; i < ; i ++)
num[i] = i+;
return combinationSum2(num, n, k);
}
vector<vector<int> > combinationSum2(vector<int> &num, int target, int k) {
sort(num.begin(), num.end());
vector<vector<int> > ret;
vector<int> cur;
Helper(ret, cur, num, target, , k);
return ret;
}
void Helper(vector<vector<int> > &ret, vector<int> cur, vector<int> &num, int target, int position, int k)
{
if(target == )
{
if(cur.size() == k)
ret.push_back(cur);
else
;
}
else
{
for(int i = position; i < num.size() && num[i] <= target; i ++)
{
cur.push_back(num[i]);
Helper(ret, cur, num, target-num[i], i+, k);
cur.pop_back();
}
}
}
};
【LeetCode】216. Combination Sum III的更多相关文章
- 【LeetCode】216. Combination Sum III 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 方法一:DFS 方法二:回溯法 日期 题目地址:h ...
- 【刷题-LeetCode】216. Combination Sum III
Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...
- 【LeetCode】170. Two Sum III – Data structure design
Difficulty:easy More:[目录]LeetCode Java实现 Description Design and implement a TwoSum class. It should ...
- 【LeetCode】40. Combination Sum II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:回溯法 日期 题目地址:ht ...
- 【leetcode】437. Path Sum III
problem 437. Path Sum III 参考 1. Leetcode_437. Path Sum III; 完
- 【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】39. Combination Sum (2 solutions)
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- 【LeetCode】040. Combination Sum II
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- 【LeetCode】437. Path Sum III 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS + DFS BFS + DFS 日期 题目地 ...
随机推荐
- python3 CERTIFICATE_VERIFY_FAILED错误 certificate verify failed
在response = request.urlopen(url)打开一个https连接时报如下错误: urllib.error.URLError: <urlopen error [SSL: CE ...
- AVR单片机命名规则
ATmega64 TQFP封装现主要有以下型号:ATmega64L-8AU.ATmega64L-8AI.ATmega64-16AU.ATmega64-16AI. 型号标识说明: (1)带"L ...
- [Docker] Linking Node.js and MongoDB Containers
To do communcation between containers, we need to do link between containers. 1. Run a container wit ...
- linux设置开机同步时间
在/etc/init.d/下新建zhjdate脚本,添加如下内容: #!/bin/bash# chkconfig: 345 63 37#chkconfig:345 63 37 (数字345是指在运行级 ...
- spring MVC、mybatis配置读写分离,ReplicationDriver(转载)
参考:http://shift-alt-ctrl.iteye.com/blog/2271730c 环境: 3台数据库机器,一个master,二台slave,分别为slave1,slave2 2.要实现 ...
- iOS编程(双语版)-视图-Frame/Bounds/Center
1. Frame 每个视图都有一个frame属性,它是CGRect结构,它描述了视图所在的矩形在其父视图中的位置. (屏幕坐标系默认的原点在左上角,x轴向右伸展,y轴向下伸展) 设置frame通常通过 ...
- SpeechLib 应用
//引用组件:Interop.SpeechLib.dll //导入空间:SpeechLib //引用组件:Interop.SpeechLib.dll//导入空间:SpeechLib //1.SpVoi ...
- 压力测试 JMeter3.3
历史下载版本 https://archive.apache.org/dist/jmeter/source/
- C语言操作Redis总结
#include "hiredis.h" #define NO_QFORKIMPL #pragma comment(lib,"hiredis.lib") #pr ...
- Keepalived系列一:Keepalived.conf 详解
接上一篇博客: http://www.cnblogs.com/xiaoit/p/4499703.html 1:按照上篇博客安装后的配置文件在 /usr/local/etc/keepalived/kee ...