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:

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]]

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

class Solution {
public:
vector<vector<int>> combinationSum3(int k, int n) {
vector<vector<int>> ret;
vector<int> path;
int target = ;
helper(k, n, , , target, path, ret);
return ret;
}
void helper(const int k ,const int n, int i_th, int prev, int& target, vector<int>& path, vector<vector<int>>& ret){
if(i_th == k && n == target) {
ret.push_back(path);
return ;
}
for(int i=prev+; i < ; i++){
path.push_back(i);
target += i;
helper(k, n, i_th+, i, target, path, ret);
target -= i;
path.pop_back();
}
}
};

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. 解释mysql 语句

    一.在我们创建mysql数据库的时候我们经常会用到这句SQL: CREATE DATABASE TEST DEFAULT CHARACTER SET utf8 COLLATE utf8_general ...

  2. 【Distributed】限流技巧

    一.概述 1.1 高并发服务限流特技 1.2 为什么要互联网项目要限流 1.3 高并发限流解决方案 二.限流算法 2.1 计数器 2.2 滑动窗口计数 2.3 令牌桶算法 使用RateLimiter实 ...

  3. 【python+selenium】selenium grid(分布式)

    前言 原文:https://blog.csdn.net/real_tino/article/details/53467406 Selenium grid是用来分布式执行测试用例脚本的工具,比如测试人员 ...

  4. STM32——CAN总线过滤器设置

    STM32CAN控制器每个筛选器组由两个32位的寄存器组成. 根据所需位宽的不同,各个筛选器可配置成16位或32位模式(如下图,当FSCx=1为32位模式,FSCx=0时为16位模式).同时,筛选器的 ...

  5. AJAX配置csrf

    // 从COokie取CSRF TOKEN的值 function getCookie(name) { var cookieValue = null; if (document.cookie & ...

  6. 阿里云MySQL用Navicat连接问题

    阿里云上装好MySQL用Navicat连接出现密码正确,Navicat连接测试失败.解决方案:网上众多方案,比如:放行3306端口啥子的,加入安全组啥子的.确认这些都没问题,还是出现密码正确,测试连接 ...

  7. Javascript节点选择

    jQuery.parent(expr) 找父亲节点,可以传入expr进行过滤,比如$("span").parent()或者$("span").parent(&q ...

  8. nginx解决浏览器跨域问题

    1.跨域问题 浏览器出于安全方面的考虑,只允许与本域下的接口交互.不同源的客户端脚本在没有明确授权的情况下,不能读写对方的资源. 例如访问www.test1.com 页面, 返回的文件中需要ajax向 ...

  9. String、StringBuffer和StringBuilder区别

    String.StringBuffer和StringBuilder区别 1.长度是否可变 String 是被 final 修饰的,他的长度是不可变的,就算调用 String 的concat 方法,那也 ...

  10. Java调用和回调总结(2)

    Java调用和回调总结(2) 调用的种类 调用有3种, 普通调用(同步调用), 异步调用, 异步回调. 三种调用的特点 普通调用: 也叫做同步调用 , 最常见的调用, 会造成阻塞. 异步调用 : 异步 ...