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. SVN中的常见错误(长期更新)

    一 svn中的简写含义. A:add,新增 C:conflict,冲突 D:delete,删除 M:modify,本地已经修改 G:modify and merGed,本地文件修改并且和服务器的进行合 ...

  2. ExtJS MVC学习手记

    开始学习ExtJS的MVC了.这篇文章仅是用来做一个目录,为自己这个阶段的学习内容做个索引. 手记涉及的文章: EXTJS MVC结构(译自ExtJS4.0文档中的<MVC Architectu ...

  3. [转]"error while loading shared libraries: xxx.so.x" 错误的原因和解决办法

    [转]"error while loading shared libraries: xxx.so.x" 错误的原因和解决办法 http://blog.csdn.net/sahuso ...

  4. MVC3+中 ViewBag、ViewData和TempData的使用和区别

    在MVC3开始,视图数据可以通过ViewBag属性访问,在MVC2中则是使用ViewData.MVC3中保留了ViewData的使用.ViewBag 是动态类型(dynamic),ViewData 是 ...

  5. verilog实现16位五级流水线的CPU带Hazard冲突处理

    verilog实现16位五级流水线的CPU带Hazard冲突处理 该文是基于博主之前一篇博客http://www.cnblogs.com/wsine/p/4292869.html所增加的Hazard处 ...

  6. SQL Server2008 无法连接到 local

    以下这种情况: 第一步:检查是否选择的数据库引擎,然后实例名字是否正确,直接写的(local)或者打的"."号.换成电脑的计算机名字或者IP. 第二步:SQLserver配置远程连 ...

  7. "渴了么"用户场景分析

    典型用户 (1)名字:王美丽 (2)年龄:21 (3)收入:勤工助学和兼职等 (4)代表的用户在市场上的比例和重要性(比例大不等同于重要性高,如付费的用户比例较少,但是影响大,所以更重要). 作为大学 ...

  8. SASS学习笔记_01

      scss两种格式 sass    大括号 scss   css写法   Arguments:  --no-cache –style compressed --update $FileName$:c ...

  9. 【BZOJ】【1011】【HNOI2008】遥远的行星

    神奇的思路题QAQ 玛雅看到这题我就醉了,什么玩意……5%的误差?果断膜拜@ydc神犇的题解: 就是因为不清楚如何应用那个答案误差不超过5%啦. 从没见过这么诡异的题一下就懵了,问到了方法之后都还半信 ...

  10. 堆(heap)和栈(stack)的区别

    转: 一.预备知识―程序的内存分配 一个由c/C++编译的程序占用的内存分为以下几个部分 1.栈区(stack)― 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其操作方式类似于数据结构中 ...