【LeetCode】Combinations
2014-05-11 17:10
369 查看
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:
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 ArrayList<ArrayList<Integer>> combine(int n, int k) { if(k>n) return null; ArrayList<Integer> ai = new ArrayList<Integer>(); ArrayList<ArrayList<Integer>> re = new ArrayList<ArrayList<Integer>>(); for(int m=0;m<n;m++){ ai=new ArrayList<Integer>(); ai.add(m+1); re.add(ai); } for(int i=1;i<k;i++){ Iterator<ArrayList<Integer>> it = re.iterator(); ArrayList<ArrayList<Integer>> tempre = new ArrayList<ArrayList<Integer>>(); while(it.hasNext()){ ArrayList<Integer> temp = it.next(); int tt = temp.get(temp.size()-1); for(int j=tt+1;j<=n;j++){ ArrayList<Integer> newtemp = new ArrayList<Integer>(); newtemp.addAll(temp); newtemp.add(j); tempre.add(newtemp); } } re=tempre; } return re; } }
相关文章推荐
- Web App 讲义教程
- lamp
- nexus 4刷机出现wifi、声音无法使用情况
- CGAffineTransformMakeRotation改变了center
- static 与 extern
- 多让小孩练练“胆子”[图]
- Django框架下,后台看不到html里用户输入的用户名和密码
- 4. 类型转换
- leetcode --day12 Surrounded Regions & Sum Root to Leaf Numbers & Longest Consecutive Sequence
- 基于链表的内存池算法
- unix/linux 探讨系统调用write的原子性(atomic)
- When to use Class.isInstance() & when to use instanceof operator?
- Spring对注解(Annotation)处理源码分析2——解析和注入注解配置的资源
- Linux 2.6 menuconfig内核编译配置选项详解
- poj 3267(动态规划)
- Core Telephony
- 《Android应用开发入门教程(经典版)》源程序
- Web APP 随笔
- tomcat结合nginx或apache实现负载均衡
- cvs分配用户权限