216. Combination Sum III
来源:互联网 发布:系统解剖学网络公开课 编辑:程序博客网 时间:2024/06/07 17:48
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]]
class Solution { public List<List<Integer>> combinationSum3(int k, int n) { List<List<Integer>> res = new ArrayList<List<Integer>>(); if(k<1 || n<1) return res; List<Integer> cur = new ArrayList<Integer>(); rec(res, cur, 0, k, n, 1); return res; } private void rec(List<List<Integer>> res, List<Integer> cur, int sum, int k, int n, int level) { if(sum==n && k==0) { res.add(new ArrayList(cur)); return; } else if(sum>n || k<=0) return; for(int i=level; i<=9; i++) { cur.add(i); rec(res, cur, sum+i, k-1, n, i+1); cur.remove(cur.size() - 1); } } }
阅读全文
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
- 线程安全,有状态,无状态的对象
- 数组(API)
- MySQL高可用架构之MHA
- HDU 1011 树形 dp
- Bootstrap导航组件
- 216. Combination Sum III
- git 初学
- TP框架如何去除undefind index提示
- 分页(对全部数据进行分页)
- lisp基本语法——(一)
- ClassPath
- java:函数--返回多个值
- 大范围内高效查找回文质数(回文数猜想)
- chocolate