LeetCode:Combinations
来源:互联网 发布:js 字符串转jsonarray 编辑:程序博客网 时间:2024/06/09 14:07
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],]
这题目实现需要点小技巧,以前在ACM题中见过。
public ArrayList<ArrayList<Integer>> combine(int n, int k) {ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();if (k <= 0) {return result;}int[] kAarray = new int[k];int i = 0;kAarray[0] = 0;while (i >= 0) {kAarray[i]++;if (kAarray[i] > n) {kAarray[i] = 0;i--;} else {if (i == k - 1) {ArrayList<Integer> list = new ArrayList<Integer>(k);for (int kk : kAarray) {list.add(kk);}result.add(list);} else {kAarray[i+1] = kAarray[i];i++;}}}return result;}
- LeetCode: Combinations
- LeetCode: Combinations
- LeetCode Combinations
- [Leetcode] Combinations
- [LeetCode] Combinations
- Leetcode: Combinations
- LeetCode Combinations
- [Leetcode] Combinations
- [Leetcode] Combinations
- [leetcode] Combinations
- [LeetCode]Combinations
- [leetcode]Combinations
- LeetCode-Combinations
- [leetcode] Combinations
- LeetCode - Combinations
- 【Leetcode】Combinations
- Leetcode: Combinations
- 【leetcode】Combinations
- expandableListview 的使用,效果很棒
- bcm4330蓝牙分析总结
- 错误:void 值未如预期地被忽略
- Hessian入门(与Spring集成)
- Apache 实现Web Server 负载均衡,不考虑Session版
- LeetCode:Combinations
- LPTSTR、LPCSTR、LPCTSTR、LPSTR之间的转换
- Android工程师面试准备知识点
- Mybatis学习(一)
- 基于storm和hadoop的广告系统研究【3】
- Java优化
- 设置ListCtrl列表控件其中某一行的字体和背景颜色
- winform listbox 没有listitem的问题
- java日历类处理