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

解题思路:

偷懒一点,直接在Java for LeetCode 040 Combination Sum II上面再加一个depth即可,JAVA实现如下:

  1. public List<List<Integer>> combinationSum3(int k, int n) {
  2. int[] candidates = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  3. ArrayList<List<Integer>> list = new ArrayList<List<Integer>>();
  4. if (k > 9 || n > 55 || n / k == 0)
  5. return list;
  6. dfs(list, candidates, 0, n, 0, k, 0);
  7. return list;
  8. }
  9.  
  10. static List<Integer> list2 = new ArrayList<Integer>();
  11.  
  12. static void dfs(ArrayList<List<Integer>> list, int[] array, int result,
  13. int target, int depth, int k, int depth2) {
  14. if (result == target && depth2 == k) {
  15. list.add(new ArrayList<Integer>(list2));
  16. return;
  17. } else if (depth2 >= k || result > target || depth >= array.length)
  18. return;
  19. for (int i = 0; i <= 1; i++) {
  20. for (int j = 0; j < i; j++) {
  21. list2.add(array[depth]);
  22. depth2++;
  23. }
  24. dfs(list, array, result + array[depth] * i, target, depth + 1, k,
  25. depth2);
  26. for (int j = 0; j < i; j++) {
  27. list2.remove(list2.size() - 1);
  28. depth2--;
  29. }
  30. }
  31. }

Java for LeetCode 216 Combination Sum III的更多相关文章

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

  2. LeetCode 216. Combination Sum 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 from ...

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

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

  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】216. Combination Sum III

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

  7. LC 216. Combination Sum III

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

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

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

  9. Java实现 LeetCode 216. 组合总和 III(三)

    216. 组合总和 III 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. 解集不能包含重复的组合. ...

随机推荐

  1. sql server 日期相关操作

    ), ): ), ): :57AM ), ): ), ): ), ): ), ): ), ): ), ): ), ): , ), ): :: ), ): :::827AM ), ): ), ): ), ...

  2. 使用emmet如何生成lipsum的随机内容

    emmet不会解析(即扩展)大括号中的内容, 它只是把大括号中的内容当成纯粹的字符串, 当成 literal的文本, 不会当成lipsum的缩写, 不会进行扩展. 要扩展, 就必须把lorem, li ...

  3. GOF业务场景的设计模式-----策略模式

    定义:定义一组算法,将每个算法都封装起来,并且使他们之间可以互换. 策略模式代码实现 interface IStrategy { public void doSomething(); } class ...

  4. 使用jasmine来对js进行单元测试

    互联网的快速发展,给web开发人员带来了前所未有的挑战.对于前端开发,前端开发er所需要编写的js早已不是那些寥寥几行的视觉效果代码.代码量的大增,多人协同,人员素质悬殊不齐,这都需要一个标准,来对代 ...

  5. 2015年12月01日 GitHub入门学习(一)GitHub简介

    序:Github理念是Social Coding(社会化编程).octocat是它的吉祥物. 一.Github与Git的区别与联系 区别:GIT是仓库,Github是提供一种将代码提交到Git仓库的服 ...

  6. 欧几里得证明$\sqrt{2}$是无理数

    选自<费马大定理:一个困惑了世间智者358年的谜>,有少许改动. 原译者:薛密 \(\sqrt{2}\)是无理数,即不能写成一个分数.欧几里得以反证法证明此结论.第一步是假定相反的事实是真 ...

  7. AlwaysOn可用性组功能测试(一)--AlwaysOn故障转移测试

    具体测试环境请参考: AlwaysOn可用性组测试环境安装与配置(一)--SQL群集环境搭建 AlwaysOn可用性组测试环境安装与配置(二)--AlwaysOn配置(界面与T-SQL) 一. Alw ...

  8. 71 mac boook pro 无 gpu 下caffe 安装

    71 mac boook pro 无 gpu 下caffe 安装 1.首先安装homebrew工具,相当于Mac下的yum或apt ruby -e "$(curl -fsSL https:/ ...

  9. 开发板支持wifi

    参考网址: http://wangye.org/blog/archives/845/ http://blog.csdn.net/lme525/article/details/37762519  htt ...

  10. 如何把你的图标转换成web字体

    在这篇教程中,我们将使用一个免费的Web应用程序IcoMoon将矢量图转换成Web字体,然后将生成的字体通过css应用到Web页面中. 通常我们在网站中必不可少的会使用到一些小图标.在正常尺寸下,布局 ...