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

题目大意:从1~9找出所有的k个数的组合,使其和等于n。

解题思路:还是回溯,每个数只能出现一次,每次递归都从当前数的下一个开始,当k==0&&n==0,就可以加入结果集。

    public List<List<Integer>> combinationSum3(int k, int n) {
List<List<Integer>> res = new ArrayList<>();
Deque<Integer> tmp = new ArrayDeque<>();
if (n == 0 || k == 0 || n / k > 9) {
return res;
}
helper(res, tmp, 1, k, n);
return res;
} private void helper(List<List<Integer>> res, Deque<Integer> tmp, int start, int k, int n) {
if (0 == n && k == 0) {
res.add(new ArrayList<>(tmp));
// System.out.println(tmp);
return;
}
for (int i = start; i <= 9; i++) {
tmp.addLast(i);
helper(res, tmp, i + 1, k - 1, n - i);
tmp.removeLast();
}
}

Combination Sum III —— LeetCode的更多相关文章

  1. Combination Sum III - LeetCode

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

  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

    [题目] Find all possible combinations of k numbers that add up to a number n, given that only numbers ...

  4. Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III)

    Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III) 同类题目: Leetcode之回溯法专题-39. 组合总数(Combination Sum) Lee ...

  5. 【LeetCode】216. Combination Sum III

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

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

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

  7. Leetcode题解(4):L216/Combination Sum III

    L216: Combination Sum III Find all possible combinations of k numbers that add up to a number n, giv ...

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

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

  9. Combination Sum,Combination Sum II,Combination Sum III

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

随机推荐

  1. CakePHP的blog教程三

    简单的身份验证和授权应用 接着我们blog教程的例子,如果我们想要建立一个根据登录的用户身份来决定其安全访问到正确的urls. 同时我们还有其他的需求: 允许我们的blog有多个作者,每一个作者都可以 ...

  2. U3D 背景音效和事件触发音效

    首先,想要在一个游戏添加背景音乐其实很简单,就是利用一个组件 就能够实现音频的播放,不过要实现通过某一个事件,来进行声音的播放,比如跳跃啊什么的: public AudioClip jumpclips ...

  3. 网站出现 HTTP 错误 401.2 - 未经授权:访问由于服务器配置被拒绝

    原因:关闭了匿名身份验证 解决方案: 在开始菜单中输入运行->inetmgr,打开站点属性->目录安全性->身份验证和访问控制->选中"启用匿名访问",输入 ...

  4. ORACLE添加作业

    --创建job declare job number; beginsys.dbms_job.submit(job,'prc_into_actiwager;',sysdate,'sysdate+30/( ...

  5. Android开发---支付宝功能接口(支付功能)(转载!)

    最近在做一个关于购物商城的项目,项目里面付款这块我选的是调用支付宝的接口,因为用的人比较多. 在网上搜索了以下,有很多这方面的教程,但大部分教程过于陈旧,而且描述的过于简单.而且支付宝提供的接口一直在 ...

  6. Fluent NHibernate之旅

    Fluent NHibernate 之旅 导航篇: [原创]Fluent NHibernate之旅开篇: [原创]Fluent NHibernate之旅二--Entity Mapping: [原创]F ...

  7. 用java写bp神经网络(三)

    孔子曰,吾日三省吾身.我们如果跟程序打交道,除了一日三省吾身外,还要三日一省吾代码.看代码是否可以更简洁,更易懂,更容易扩展,更通用,算法是否可以再优化,结构是否可以再往上抽象.代码在不断的重构过程中 ...

  8. Swift中共有74个内建函数

    Swift中共有74个内建函数,但是在Swift官方文档(“The Swift Programming Language”)中只记录了7中.剩下的67个都没有记录.   本文将列举Swift所有的内建 ...

  9. JavaScript学习笔记之原型对象

    本文是学习<JavaScript高级程序设计>第六章的笔记. JS中,便于批量创建对象的三种模式: 1.工厂模式:用一个函数封装创建对象的细节,传入必要的参数,在函数内部new一个对象并返 ...

  10. Vijos1825 NOI2008 志愿者招募 费用流

    Orz ByVoid大神的题解:https://www.byvoid.com/blog/noi-2008-employee/ 学习网络流建图的好题,不难想到线性规划的模型,不过利用模型的特殊性,结合网 ...