77. Combinations
来源:互联网 发布:淘宝手机单怎么刷 编辑:程序博客网 时间:2024/05/17 08:59
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],]
Subscribe to see which companies asked this question
public class Solution { public List<List<Integer>> combine(int n, int k) { List <Integer> r = new ArrayList <Integer> (); List <List<Integer>> ret = new ArrayList <List<Integer>> (); if(k>n)return ret; for(int i = 1;i<=k;i++){ r.add(i); } int cur = k-1; List <Integer> c = new ArrayList <Integer> (); c.addAll(r); ret.add(c); // r.remove(0); // r.add(2); // ret.add(r); // return ret; while(true){ if(r.get(cur)==(n-k+cur+1)){ while(r.get(cur)==(n-k+cur+1)){ cur--; if(cur<0)return ret; } for(int i = k-1;i>cur;i--){ r.remove(i); } int a = r.get(cur); r.remove(cur); for(int i = cur;i<=k-1;i++){ a++; r.add(a); } cur = k-1; List <Integer> x = new ArrayList <Integer> (); x.addAll(r); ret.add(x); continue; }else{ int b =r.get(cur); r.remove(cur); r.add(b+1); List <Integer> x = new ArrayList <Integer> (); x.addAll(r); ret.add(x); } } }}
0 0
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 2014年06月12日
- 织梦Dedecms网站安全优化之重定义目录
- 开机之后桌面没有图标
- 教程讲解dedecms动态编译式模版引擎
- windows 正在运行文件,可以等windows工作完或者先关闭文件后再退出windows”
- 77. Combinations
- 黑客如何快速查找网站后台地址方法整理
- PHP获取 ,根目录,当前文件路径,上层目录路径 ,文件名,扩展名
- is_file(\'images/xx.gif\'); 能找到,is_file(\'/images/xxx.gif\')就找不到了fi le_exists()
- phpMyAdmin“无法载入mysql扩展,请检查PHP配置
- WINDOWS7IIS7.5配置PHP环境
- HTML5开发移动APP原理介绍
- 请问屏幕保护程序对液晶显示器有没有损害?
- iss6 iis7 iis7.5 apache 设置脚本权限方法。