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. 【Java】Java-ShutDownHook-优雅关闭系统资源

    Java-ShutDownHook-优雅关闭系统资源 java shuadownhook_百度搜索 Java应用中使用ShutdownHook友好地清理现场 - 残雪余香 - 博客园 java kil ...

  2. RAMPS1.4 3d打印控制板接线与测试3

    RAMPS1.4作为mega2560的拓展板插在mega板子上面.从而让mega板子可以控制3d打印机的工作.ramps上的接线至关重要,接错不仅不能打印,甚至还会烧坏器件和板子.请一定注意. 我的淘 ...

  3. Linux中挂载新的磁盘到指定目录或分区

    新增磁盘的设备文件名为 /dev/vdb 大小为100GB. #fdisk -l  查看新增的的磁盘 1.对新增磁盘进行分区 #fdisk /dev/vdb 按提示操作 p打印  n新增 d 删除 w ...

  4. Angular 2的12个经典面试问题汇总(文末附带Angular測试)

    Angular作为眼下最为流行的前端框架,受到了前端开发者的普遍欢迎.不论是初学Angular的新手.还是有一定Angular开发经验的开发者,了解本文中的12个经典面试问题,都将会是一个深入了解和学 ...

  5. 【指导】SonarQube 部署说明

    转载:https://blog.csdn.net/cuiaamay/article/details/52057091 1,安装 1.1 安装依赖 需要保证Oracle JRE 8 及以上,或者 Ope ...

  6. 【python】安装bencode

    C:\Users\horn1\Desktop\python\35-pytorrent>pip install bencodeCollecting bencode Downloading http ...

  7. Windows下搭建elasticsearch集群案例

    https://blog.csdn.net/u014236259/article/details/64129918

  8. iOS开发技巧 - 使用和定制开关控件(UISwitch)

    1. 初始化加载到视图界面 (Swift) import UIKit class ViewController: UIViewController { // 1. create a property ...

  9. 【树莓派】使用VNC进行远程控制

    之前有进行过VNC以及xrdp连接树莓派,并成功了. 这里看到一篇比较新的,基于mac的连接,文章转载收藏,实践可参考. 这一课里我们将学习如何在树莓派上安装和使用VNC.它可以使你通过图形界面的方式 ...

  10. java 从零开始,学习笔记之基础入门<集合>(十六)

    集合 集合:将多个元素放入到一个集合对象中去,对应的集合对象就可以用来存储多元素. Collection接口的子接口:Set接口和List接口. Map不是Collection接口的子接口. Coll ...