给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合。
例如,
如果 n = 4 和 k = 2,组合如下:
[
  [2,4],
  [3,4],
  [2,3],
  [1,2],
  [1,3],
  [1,4],
]
详见:https://leetcode.com/problems/combinations/description/

Java实现:

class Solution {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> res=new ArrayList<List<Integer>>();
List<Integer> out=new ArrayList<Integer>();
helper(n,k,1,out,res);
return res;
}
private void helper(int n,int k,int start,List<Integer> out,List<List<Integer>> res){
if(out.size()==k){
res.add(new ArrayList<Integer>(out));
}
for(int i=start;i<=n;++i){
out.add(i);
helper(n,k,i+1,out,res);
out.remove(out.size()-1);
}
}
}

参考:http://www.cnblogs.com/grandyang/p/4332522.html

077 Combinations 组合的更多相关文章

  1. [FollowUp] Combinations 组合项

    这是Combinations 组合项 的延伸,在这里,我们允许不同的顺序出现,那么新的题目要求如下: Given two integers n and k, return all possible c ...

  2. [LeetCode] Combinations 组合项

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  3. combinations(组合)

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  4. 【LeetCode每天一题】Combinations(组合)

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...

  5. leetCode 77.Combinations (组合)

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  6. [Leetcode] combinations 组合

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  7. Java for LeetCode 077 Combinations

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  8. [leetcode]77. Combinations组合

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...

  9. Leetcode77. Combinations组合

    给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], [1,3] ...

随机推荐

  1. 中文标准web字体

    标准的简体中文web字体: Windows OS X 黑体:SimHei 冬青黑体: Hiragino Sans GB [NEW FOR SNOW LEOPARD] 宋体:SimSun 华文细黑:ST ...

  2. <十八>UML核心视图动态视图之协作图

    一:协作图 --->描述了对象间交互的一种模式.它通过对象之间的连接和它们相互发送的消息来显示参与交互的对象 --->协作图可以有对象和主角实例,以及描述它们之间关系和交互的连接和消息.通 ...

  3. 1070 Bash 游戏 V4

    传送门 1070 Bash游戏 V4   基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有一堆石子共有N个.A B两个人轮流拿,A先拿.每次拿的数量最少1个,最多 ...

  4. AtCoder Grand Contest 014 D:Black and White Tree

    题目传送门:https://agc014.contest.atcoder.jp/tasks/agc014_d 题目翻译 给你一棵树,每次任选一个点染色,先手染白色,后手染黑色.如果最后存在一个白色的点 ...

  5. Poj_1011_Sticks(剪枝)

    一.Description 乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位.然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度. ...

  6. javascript 日期月份加减

    项目中需要用到,自己写了一个.javascript日期按月加减 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xh ...

  7. Eclipse下对maven进行配置

    前提:安装好maven插件http://www.cnblogs.com/lchzls/p/6281697.html 1.需要修改配置:首先选择Window->Preferences,弹出如下对话 ...

  8. eos管理页面

    调用此方法删除需要在po_module_processdef添加数据如下 默认情况下申请页面是有权限的 但是在此表加过之后 管理页面要打开拟稿页面还必须在   系统管理页面(xtgl.jsp )  分 ...

  9. linux下 卸载vmtools

    注意事项: 1) 安装linux时,一定要安装gcc和kernel-source: 2)光驱使用完毕可使用 umount /mnt/cdrom 卸载掉 3)VMtools 默认安装在 /usr/bin ...

  10. 2018ACM山东省赛 Games(dp取数)

    Games Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description Alice and Bob ...