216. Combination Sum III
来源:互联网 发布:孙子兵法华杉 知乎 编辑:程序博客网 时间:2024/05/22 03:27
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.
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]]
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
Seen this question in a real interview before?
public class Solution { public List<List<Integer>> combinationSum3(int k, int n) { List<List<Integer>> re = new ArrayList<List<Integer>>();helper(new ArrayList<Integer>(), re, 1, 1, k, 0, n);return re; } public void helper(List<Integer> data, List<List<Integer>> re, int level, int j, int k, int cur, int n) {if (level > k) {if (cur == n)re.add(data);return;}for (int i = j; i < 10 && (k - level + 1) * i <= n; ++i) {List<Integer> next = new ArrayList<>(data);next.add(i);helper(next, re, level + 1, i + 1, k, cur + i, n);}}}
阅读全文
0 0
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- R中ifelse、which、%in%的用法
- 使用双缓冲技术解决动画播放闪烁问题
- C++学习笔记之编程应当注意的问题
- Python操作Mysql数据库
- 每日一题——链表相交问题
- 216. Combination Sum III
- Android--Socket通信
- bingo-培训——用户体验
- 盘点8种CSS实现垂直居中水平居中的绝对定位居中技术
- ubunut python2 和python 3 同时存在 pip指向问题
- 理发师
- oracel 中关于时间日期的取值和转化
- 微信小程序 同步请求授权
- SpringData JPA中数据库表的映射类需要一个不在表中的字段