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.

Note:

  • All numbers will be positive integers.
  • The solution set must not contain duplicate combinations.

Example 1:

  1. Input: k = 3, n = 7
  2. Output: [[1,2,4]]

Example 2:

  1. Input: k = 3, n = 9
  2. Output: [[1,2,6], [1,3,5], [2,3,4]]

Runtime: 0 ms, faster than 100.00% of C++ online submissions for Combination Sum III.

  1. class Solution {
  2. public:
  3. vector<vector<int>> combinationSum3(int k, int n) {
  4. vector<vector<int>> ret;
  5. vector<int> path;
  6. int target = ;
  7. helper(k, n, , , target, path, ret);
  8. return ret;
  9. }
  10. void helper(const int k ,const int n, int i_th, int prev, int& target, vector<int>& path, vector<vector<int>>& ret){
  11. if(i_th == k && n == target) {
  12. ret.push_back(path);
  13. return ;
  14. }
  15. for(int i=prev+; i < ; i++){
  16. path.push_back(i);
  17. target += i;
  18. helper(k, n, i_th+, i, target, path, ret);
  19. target -= i;
  20. path.pop_back();
  21. }
  22. }
  23. };

LC 216. Combination Sum III的更多相关文章

  1. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

  2. [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 ...

  3. 【LeetCode】216. Combination Sum III

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

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

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

  5. 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV

    ▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...

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

  7. Leetcode 216. Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  8. 216. Combination Sum III——本质DFS

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  9. 216. Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

随机推荐

  1. 根据CPU核心数确定线程池并发线程数(转)

    一.抛出问题 关于如何计算并发线程数,一般分两派,来自两本书,且都是好书,到底哪个是对的?问题追踪后,整理如下: 第一派:<Java Concurrency in Practice>即&l ...

  2. davinci 删除路线站点关系

    删除路线站点关系 DELETE FROM tb_station_info_draw WHERE id in (SELECT stationId FROM tb_road_station_relatio ...

  3. 玩转springcloud(二):注册中心-Eureka

    一.简介 注册中心 注册中心是服务发现的核心.它保存了各个可用服务实例的网络地址(IP Address和Port).服务注册中心必须要有高可用性和实时更新功能. Netflix Eureka 就是一个 ...

  4. Kinect视频中运用全身运动和人体测量统计学的人物识别技术

    摘要: 对于人物识别技术来说,动作和人体测量统计学对于光学差异并不敏感,甚至对于眼镜,头发,帽子的描述相当粗糙,现在的以步态为基础的识别技术都是基于对细节的精确描述和对步态周期的精确测量.这种方法需要 ...

  5. 【CQOI2017】老C的方块

    Description https://loj.ac/problem/3022 Solution 他讲得很清楚 将那篇博客中的红色标号为 \(0\),黄色为 \(1\),蓝色为 \(2\),绿色为 \ ...

  6. C# ado.net 操作(一)

    简单的增删改查 class Program { private static string constr = "server=.;database=northwnd;integrated s ...

  7. mysql数据库系统学习(一)---一条SQL查询语句是如何执行的?

    本文基于----MySQL实战45讲(极客时间----林晓斌 )整理----->https://time.geekbang.org/column/article/68319 一.第一节:一条sq ...

  8. 12、redis部分

  9. Visual Stdio的使用

    以下基于vs2017版本 part 1: 问题及解决 1.命令窗口一闪而过 右键项目,选择属性--连接器---系统---子系统---选择控制台. 2.修改默认启动项目 右键解决方案,选择属性,选择当前 ...

  10. 排列组合C、A

    排列组合是组合学最基本的概念.所谓排列,就是指从给定个数的元素中取出指定个数的元素进行排序.组合则是指从给定个数的元素中仅仅取出指定个数的元素,不考虑排序. 排列组合定义及公式 排列的定义:从n个不同 ...