Add to List 77. Combinations
来源:互联网 发布:软件测试计划书实例 编辑:程序博客网 时间:2024/06/16 01:39
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
For example,
If n = 4 and k = 2, a solution is:
[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]
public class Solution { public List<List<Integer>> combine(int n, int k) { List<List<Integer>> data = new ArrayList<>();if (n == 0 || n < k)return data;helper(data, new LinkedList<Integer>(), n, k);return data; } public void helper(List<List<Integer>> data, List<Integer> temp, int n, int k) {for (int i = n; i >= k; --i) {if (k == 0) {data.add(temp);break;} else {LinkedList<Integer> next = new LinkedList<>(temp);next.addFirst(i);helper(data, next, i - 1, k - 1);}}}}
阅读全文
0 0
- Add to List 77. Combinations
- Leetcode-Letter Combinations of a Phone Number Add to List
- Add to List 67
- Add to List 67. Add Binary
- TortoiseSVN add to ignore list
- LeetCode 143 Reorder List Add to List
- leetcode148~Sort List Add to List
- Add to List 147. Insertion Sort List
- LeetCode: Add to List 148. Sort List
- Android Add To System Share List
- 414. Third Maximum Number Add to List
- 53. Maximum Subarray Add to List
- leetcode41~First Missing Positive Add to List
- leetcode 500 Keyboard Row Add to List
- Leetcode 494. Target Sum Add to List
- 461.Hamming Distance Add to List
- 310. Minimum Height Trees Add to List
- 476. Number Complement Add to List
- mysql 死锁日志查看
- dirname(__FILE__) 与 PHP define() 及例子define('root',dirname(__file__)) (2011-04-01 11:09:48)转载▼ 标签: di
- 光耦测试之频率篇
- Active/Passive MySQL High Availability Pacemaker Cluster with DRBD on CentOS 7
- OpenFlow
- Add to List 77. Combinations
- js 小知识
- ubuntu 14.04 ROS + kinect v2 driver安装方法;安装驱动时遇到的问题及解决方法(1)
- #1366
- git stash 的应用
- Online Object Tracking : A Benchmark
- odoo db_filter 参数设置问题,造成不能显示数据库
- tp5的ajax返回 控制器的操作
- java 经典书籍和一些好网站