https://leetcode.com/problems/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]]

解题思路:

新题,与前面的系列相比,简单了一些,常规的dfs,没有啥好说的。

public class Solution {
public List<List<Integer>> combinationSum3(int k, int n) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
List<Integer> cur = new ArrayList<Integer>();
dfs(k, n, res, cur, 1, 0);
return res;
} public void dfs(int k, int n, List<List<Integer>> res, List<Integer> cur, int step, int sum) {
if(cur.size() == k && sum == n) {
res.add(new ArrayList(cur));
return;
}
if(cur.size() > k) {
return;
}
if(cur.size() <= k && sum > n) {
return;
}
for(int i = step; i < 10; i++) {
cur.add(i);
sum += i;
dfs(k, n, res, cur, i + 1, sum);
cur.remove(cur.size() - 1);
sum -= i;
}
}
}

//20181013

class Solution {
public List<List<Integer>> combinationSum3(int k, int n) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
List<Integer> cur = new ArrayList<Integer>();
dfs(res, cur, k, n, 1, 0);
return res;
} public void dfs(List<List<Integer>> res, List<Integer> cur, int k, int n, int start, int sum) {
if (cur.size() == k && sum == n) {
res.add(new ArrayList(cur));
} for (int i = start; i <= Math.min(n, 9); i++) {
cur.add(i);
dfs(res, cur, k, n, i + 1, sum + i);
cur.remove(cur.size() - 1);
}
}
}

Combination Sum III的更多相关文章

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

  2. [Leetcode 216]求给定和的数集合 Combination Sum III

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

  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 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

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

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

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

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

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

  8. LC 216. Combination Sum III

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

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

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

  10. [LeetCode] Combination Sum III 组合之和之三

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

随机推荐

  1. Android--将Bitmip转化成字符串

    因为自己做的东西想要上传到服务器,所以就选择了将Bitmip转化成了字符串在上传 其它格式的图片我们好像可以用Bitmap.Factory 去将他们转化成BitMap 转化成字符串的代码 //将bit ...

  2. 海蜘蛛ISPV6.1.5,目前破解版本中最稳定的!

    海蜘蛛ISPV6.1.5,目前破解版本中最稳定的! 破解步骤如下: 一.安装完毕进控制台 二.使用muddyboot登陆 密码(123456) 三.输入root回车 四.输入regtools回车 五. ...

  3. ActiveX控件的基本操作方法以及如何在VS2010下使用控件

    在此,小编就介绍下ActiveX控件的基本操作方法以及如何在VS2010下使用控件,我们以一个程序为例, (1)      打开VS2010编译器(右键以管理员身份运行,因为ActiveX需要注册), ...

  4. To add private variable to this Javascript literal object

    You can use number as function/variable name, the numberic name can't be accessed from parent scope, ...

  5. oracle查看最大长度

    select s.ids from Student s where length(s.ids)=311 select max(length(s.ids)) from Student s

  6. 阴影 box-shadow(一)

    阴影 box-shadow(一) box-shadow是向盒子添加阴影.支持添加一个或者多个. 很简单的一段代码,就实现了投影效果,酷毙了.我们来看下语法: box-shadow: X轴偏移量 Y轴偏 ...

  7. 多种方法实现H5网页图片动画效果;

    在web开发中,GIF动画效果是随处可见,比如常见的loading加载.人物奔跑的gif图片等等,那么这些都是怎么实现的呢?其实实现的原理很简单,简而言之,这些所谓的动画都是一帧一帧的图片经过一段时间 ...

  8. 查看python selenium的api

    打开命令行工具,doc中输入: python -m pydoc -p 然后在浏览器中访问http://localhost:4567/,此时应该可以看到python中所有的Modules 按ctrl+f ...

  9. MySQL中求年龄

    时间函数: 1.curdate() --- 当前系统日期 调取: select curdate() 2.curtime() --- 当前系统时间 调取: select curtime() 3.now( ...

  10. 重新认识Box Model、IFC、BFC和Collapsing margins

    尊重原创,转载自: http://www.cnblogs.com/fsjohnhuang/p/5259121.html 肥子John^_^ 前言   盒子模型作为CSS基础中的基础,曾一度以为掌握了I ...