【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 日期 题目地 ...
随机推荐
- MFC中位图的显示
分析: 首先,我们要明确一点,窗口的绘制包括两个步骤,首先:擦除窗口背景,然后再对窗口重新进行绘制:当擦除窗口背景时,程序会发生一个WM_ERASEBKGND消息,因此可以在此响应函数中完成位图的显示 ...
- 用GibbsLDA做Topic Modeling
http://weblab.com.cityu.edu.hk/blog/luheng/2011/06/24/%E7%94%A8gibbslda%E5%81%9Atopic-modeling/#comm ...
- sql 根据另一个表的数据更新当前表
--update mchk --set shwdjh=dbo.erpzhong.zhongbz--from erpzhong--where mchk.dwmch=erpzhong.matno
- ASP入门(十二)-Application对象
在一起协同工作以完成某项任务的一组ASP文件称为一个应用程序.Application 对象用于把这些文件捆绑在一起. Application 对象用于在整个应用程序生存期间保存信息. Applicat ...
- [Canvas]走近的女孩
动态效果请点此下载文件并使用Chrome或者FireFox浏览器观看. 图例: 代码: <!DOCTYPE html> <html lang="utf-8"> ...
- 一个小栗子聊聊JAVA泛型基础
背景 周五本该是愉快的,可是今天花了一个早上查问题,为什么要花一个早上?我把原因总结为两点: 日志信息严重丢失,茫茫代码毫无头绪. 对泛型的认识不够,导致代码出现了BUG. 第一个原因可以通过以后编码 ...
- [海蜘蛛] 海蜘蛛 V8 全线无限试用版 免费发布破解教程
http://bbs.p52.cn/forum.php?mod=viewthread&tid=3499&extra=page%3D1&page=1&_dsign=79c ...
- HTTP 响应实体主体:XML 及 XML parser
本文内容 HTTP 响应实体主体:XML XML parser 总结 各编程语言实现的 XML parser HTTP 响应实体主体:XML 实体主体(entity-body)通常是HTTP响应里 ...
- maven 添加jar到中央/远程仓库
maven 添加jar到中央/远程仓库 开源中国 发表于 2014-08-23 00:08:00 commond: mvn deploy:deploy-file -DgroupId=com.tima. ...
- Android中创建option menu
1.首先在res目录下新建一个menu文件夹,右击res目录->New->Directory,输入文件夹名menu,点击OK. 接着在这个文件夹下再新建一个名叫main的菜单文件,右击me ...