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实现如下:

public List<List<Integer>> combinationSum3(int k, int n) {
int[] candidates = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
ArrayList<List<Integer>> list = new ArrayList<List<Integer>>();
if (k > 9 || n > 55 || n / k == 0)
return list;
dfs(list, candidates, 0, n, 0, k, 0);
return list;
} static List<Integer> list2 = new ArrayList<Integer>(); static void dfs(ArrayList<List<Integer>> list, int[] array, int result,
int target, int depth, int k, int depth2) {
if (result == target && depth2 == k) {
list.add(new ArrayList<Integer>(list2));
return;
} else if (depth2 >= k || result > target || depth >= array.length)
return;
for (int i = 0; i <= 1; i++) {
for (int j = 0; j < i; j++) {
list2.add(array[depth]);
depth2++;
}
dfs(list, array, result + array[depth] * i, target, depth + 1, k,
depth2);
for (int j = 0; j < i; j++) {
list2.remove(list2.size() - 1);
depth2--;
}
}
}

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. (2)apply函数及其源码

      本文原创,转载请注明出处,本人Q1273314690(交流学习) 总结: 就是MARGIN决定了你的FUN调用几次,每次传递给你的是什么维度的内容,而...是传递给FUN的(每次调用的时候都会被传 ...

  2. 【Bootstrap】Bootstrap和Java分页-第二篇

    目录 关于此文 配置xml-pager.tld 分页控件-Pager 分页action集成类-BaseController 实例-Dao 实例-service 实例-action 实例-JSP 实例- ...

  3. UVA1376.Animal Run (最小割转为最短路 && dijkstra)

    Animal Run Time Limit:6000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status ...

  4. MySQL数据库的事务管理

    当前在开发ERP系统,使用到的数据库为Mysql.下面介绍下如何开启事务,以及事务隔离的机制 : 1. 检查当前数据库使用的存储引擎. show engines; 2. 修改前my.ini中的文件如下 ...

  5. 《深入浅出WPF》笔记四

    1.WPF资源分布:数据库.资源文件.WPF对象资源.变量2.每个WPF的界面都具有一个名为Resources的属性,其类型为ResourceDictionary,以键值对的形式存储资源.3.检索资源 ...

  6. 第2月第1天 命令(Command)模式

    http://www.tracefact.net/Design-Pattern/Command.aspx 命令模式把一个请求或者操作封装到一个对象中.命令模式允许系统使用不同的请求把客户端参数化,对请 ...

  7. svn更改默认服务启动目录

    配置文件位于 /etc/sysconfig/svnserve 修改为自己的目录

  8. BZOJ2809——[Apio2012]dispatching

    1.题目大意:给一棵树和M值,每个点有两个权值C和L,选x个点,这x个点的C值的和不能超过M,且这x个点如果都在某个子树内 定义满意度为x*这个子树的根的L值 2.分析:这是一道可并堆的题目,我们考虑 ...

  9. cvLoadImage

    编辑 本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 函数原型:IplImage* cvLoadImage( const char* filename, int flags= ...

  10. echarts之字符云tooltip显示混乱问题的解决办法

    echarts字符云中tooltip显示混乱主要表现为一下两点: 1.字体与其显示框内容不对应鼠标识别错误 解决思路: 就是option里的数据要对value降序排序(这一点很关键,是必须的一步) 把 ...