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的更多相关文章

  1. 【LeetCode】216. Combination Sum III 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 方法一:DFS 方法二:回溯法 日期 题目地址:h ...

  2. 【刷题-LeetCode】216. Combination Sum III

    Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...

  3. 【LeetCode】170. Two Sum III – Data structure design

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Design and implement a TwoSum class. It should ...

  4. 【LeetCode】40. Combination Sum II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:回溯法 日期 题目地址:ht ...

  5. 【leetcode】437. Path Sum III

    problem 437. Path Sum III 参考 1. Leetcode_437. Path Sum III; 完

  6. 【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 ...

  7. 【LeetCode】39. Combination Sum (2 solutions)

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  8. 【LeetCode】040. Combination Sum II

    题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...

  9. 【LeetCode】437. Path Sum III 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS + DFS BFS + DFS 日期 题目地 ...

随机推荐

  1. MFC中设备描述表dc的使用

    以下代码我是在View类中实现的: /** 利用平台SDK实现画线功能 // 首先获得窗口的设备描述表 HDC hdc; hdc = ::GetDC( m_hWnd ); //调用的是平台SDK的成员 ...

  2. 【GRPC】GRPC-负载均衡

    GRPC-负载均衡 grpc nginx_百度搜索 grpc(1):Centos 安装java的grpc服务,使用haproxy进行负载均衡,nginx不支持 - freewebsys的专栏 - CS ...

  3. (转)C#中的委托(Delegate)和事件(Event)

    转自:http://blog.chinaunix.net/uid-576762-id-2733751.html   把C#中的委托(Delegate)和事件(Event)放到现在讲是有目的的:给下次写 ...

  4. windows系统tomcat日志输出至catalina.out配置说明

    转自:https://blog.csdn.net/liubowin/article/details/48001947 1.修改bin/startup.bat文件 修改前:call "%EXE ...

  5. angular5 基于ngx-translate实现多语言切换

    angular的坑永远都是那么多,当然了,主要还是我太菜~ 基于ngx-translate实现多语言切换这个功能,我又是折腾了很久,下面是我实现的过程: 1.安装ngx-translate 需要安装@ ...

  6. 牛客网-《剑指offer》-包含min函数的栈

    题目:http://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49 辅助栈 C++ class Solution { public ...

  7. 理解CPU steal time

    http://melody-dc.com/2015/11/21/%E7%90%86%E8%A7%A3CPU-steal-time/ http://www.cnblogs.com/yjf512/p/33 ...

  8. MySQL查看当前用户、存储引擎、日志

    #查看MySQL的当前用户 mysql> SELECT USER(); +----------------+ | USER() | +----------------+ | root@local ...

  9. web前端开发,如何提高页面性能优化?

    内容方面: 1.减少 HTTP 请求 (Make Fewer HTTP Requests) 2.减少 DOM 元素数量 (Reduce the Number of DOM Elements) 3.使得 ...

  10. sell 项目 类目表 设计 及 创建

    1.数据库设计 2.类目表 创建 /** * 类目表 */ create table `product_category` ( `category_id` int not null auto_incr ...