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],
]

从1,。。。,n中选出k个数组成集合。

这题和  组合的和combinations sum全排列  类似

这一题跟全排列有点像,但是又不太像,不能混淆。全排列是所有数字(字母)参与排列,这里只是一部分数字参与排列。跟combinations sum很像,是差不多的原理。
在全排列中,1,2,3和3,2,1是不同的两个排列,这里算是一个。
所以,这里需要注意点:1、当list长度=k的时候,表示满足一个要求。2、每次遍历时,从剩下的元素中找元素,并不是从头开始。

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

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. 【LeetCode每天一题】Combinations(组合)

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

  4. leetCode 77.Combinations (组合)

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

  5. [Leetcode] combinations 组合

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

  6. 077 Combinations 组合

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

  7. [leetcode]77. Combinations组合

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

  8. Leetcode77. Combinations组合

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

  9. Python排列组合问题

    1.字符串的全排列 问题描述:打印出原字符串中所有字符的所有排列.——将输入字符串中的每个字符作为一个不同的字符看待,即使它们是重复的,如'aaa'应打印6次. Python可以用生成器解决: def ...

随机推荐

  1. Hibernate超简单多表操作

    所谓一对多映射 在数据库中我们通常会通过添加外键的方式将表关联起来,表现一对多的关系. 而在Hibernate中,我们则要通过在一方持有多方的集合来实现,即在"一"的一端中使用元素 ...

  2. I/O操作之文件压缩与解压

    与文件压缩与解压相关的类在java.util.zip包下 实例 //文件压缩 import java.io.File; import java.io.FileInputStream; import j ...

  3. Dynamics CRM 2013 SP1 客户表单界面上联系人subgrid上的添加现有联系人功能缺失

    CRM2013打了SP1的同学会发现一个问题,客户关联联系人的1:N关系,在表单subgrid中添加联系人时,只能新建而无法添加现有联系人,而这个现象在之前的版本中是没有的. 我们通过工具ribbon ...

  4. Android:ADB server didn't ACK或者adb server is out of date. killing解决办法

    欢迎关注公众号,每天推送Android技术文章,二维码如下:(可扫描) 出现这个原因我个人感觉有两个.一.5037端口被别的程序或者进程占用:二.adb占用的不是5037端口.很多人仅仅知道第一种二忽 ...

  5. Android进阶(一)几种网络请求方式详解

    Ref:http://blog.csdn.net/zuolongsnail/article/details/6373051 Android应用经常会和服务器端交互,这就需要手机客户端发送网络请求,下面 ...

  6. Cocos2D:塔防游戏制作之旅(六)

    现在,创建一个新的类用来表示炮塔.添加新的类文件,名称为Tower,继承于CCNode. 替换Tower.h文件为如下内容: #import "cocos2d.h" #import ...

  7. Bias and Variance 偏置和方差

    偏置和方差 参考资料:http://scott.fortmann-roe.com/docs/BiasVariance.html http://www.cnblogs.com/kemaswill/ Bi ...

  8. Java进阶(五)Junit测试

    我们在编写大型程序的时候,需要写成千上万个方法或函数,这些函数的功能可能很强大,但我们在程序中只用到该函数的一小部分功能,并且经过调试可以确定,这一小部分功能是正确的.但是,我们同时应该确保每一个函数 ...

  9. 【翻译】如何创建Ext JS暗黑主题之二

    原文:How to Create a Dark Ext JS Theme– Part 2 我已经展示了如何去开发一个精致的暗黑主题,看起来就像Spotify.在本文的第一部分,了解了Fashion.S ...

  10. 将Ext JS 6应用程序导入Web项目

    由于Ext JS 6包含了Sencha Touch,因而在应用程序结构有了些改变,Ext JS 5的方法已经不适用于新版本了.经过研究,发现6导入Web项目要比5简单. 下面来说说导入的过程. 使用S ...