给定两个整数 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. redis压力测试详解

    redis做压测可以用自带的redis-benchmark工具,使用简单,效果也比较不错. linux下一般无需下载,windows下redis-benchmark压力测试工具下载地址:http:// ...

  2. 前端多媒体(1)——获取摄像头&麦克风

    捕获视频/音频 PPT地址 长久以来,音频/视频捕获都是网络开发中的"圣杯".多年来,我们总是依赖于浏览器插件(Flash 或 Silverlight)实现这一点. 依靠 WebR ...

  3. java中路径的获取

    网上摘录 (1).request.getRealPath("/");//不推荐使用获取工程的根路径 (2).request.getRealPath(request.getReque ...

  4. python 生成特定间隔数列的方法

    (1)range() 和 xrange( )[python内置函数] range(开始,结束,间隔). 值得注意的是:生成数列最后一个数< 结束值. 返回结果类型:list,其中元素是integ ...

  5. android自定义控件(六) 刷新

    三种得到LinearInflater的方法 a. LayoutInflater inflater = getLayoutInflater(); b. LayoutInflater localinfla ...

  6. ACM学习历程—HDU 5326 Work(树形递推)

    Problem Description It’s an interesting experience to move from ICPC to work, end my college life an ...

  7. 数据结构与算法(4)----->链表、二分搜索

    1.  链表的基本概念 链表和数组一样都是一种线性结构; 数组是一段连续的存储空间; 链表空间不一定保证连续,是临时分配的; 链表的分类 按方向: 单链表:每个节点只能通过next指针指向下一个节点; ...

  8. SHOI2001化工厂装箱员——记忆化搜索

    题目:https://www.luogu.org/problemnew/show/P2530 太弱了不会用DP,于是暴搜: 每次传进一个数组c记录当前状态各种物品有多少个,枚举取哪种物品,返回最小值, ...

  9. mysql错误-修改mysql.sock位置

    在Mysql下有时候会出现mysql.sock位置错误,导致无法链接数据库. mac下报错的时候: 首先修改my.cnf 位置在/etc/my.cnf下,假如没有的话,去/usr/locate/mys ...

  10. PCL推荐的命名规范(1)

    博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=209 文件命名 所有的文件名单词之间应该用下划线隔开,例 如unordere ...