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. React Native自动化测试

    大凡做软件开发,肯定会涉及到很多的测试,本地测试,Junit测试,用例测试等,今天就来说说RN的测试. React Native的官方代码仓库里有一些测试代码,你可以在贡献代码之后回归测试一下,以检测 ...

  2. Android的DatePicker和TimePicker-android学习之旅(三十八)

    DatePicker和TimePicker简介 DatePicker和TimePicker是从FrameLayout继承而来,他们都是比较简单的组件.时间改变时间分别添加OnDateChangeLis ...

  3. UNIX环境高级编程——线程和信号

    每个线程都有自己的信号屏蔽字,但是信号的处理是进程中所有线程共享的.这意味着尽管单个线程可以阻止某些信号,但当线程修改了与某个信号相关的处理行为以后,所有的线程都必须共享这个处理行为的改变.这样如果一 ...

  4. SpriteBuilder实现2D精灵光影明暗反射效果(一)

    其实不用3D建模,用2D的图像就可以模拟3D场景中光照反射的效果. 这里我们不得不提到一个normalMap(法线图)的概念,请各位童鞋自己度娘吧,简单来说它可以使得2D表面生成一定细节程度的光照方向 ...

  5. 【一天一道LeetCode】#117. Populating Next Right Pointers in Each Node II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  6. Maya人物骨骼创建与蒙皮

    参考: HumanIK骨架的使用与蒙皮的操作 注意事项: 1. 编辑好骨骼一侧后,可删除另一侧,并使用镜像操作.镜像操作可以指定替换生成骨骼的名字中的子字符串. 2. 如果在编辑骨骼的时候由删除添加过 ...

  7. ffplay.c函数结构简单分析(画图)

    最近重温了一下FFplay的源代码.FFplay是FFmpeg项目提供的播放器示例.尽管FFplay只是一个简单的播放器示例,它的源代码的量也是不少的.之前看代码,主要是集中于某一个"点&q ...

  8. iOS中 陀螺仪/加速器 韩俊强的博客

    引进框架: #import <CoreMotion/CoreMotion.h> 定义属性初始化相关: #import "ViewController.h" #impor ...

  9. Cell自适应高度及自定义cell混合使…

    第一部分:UItableViewCellAdaptionForHeight : cell的自适应高度 第二部分:CustomTableViewCell:自定义cell的混合使用(以简单通讯录为例) = ...

  10. Socket编程实践(8) --Select-I/O复用

    五种I/O模型介绍 (1)阻塞I/O[默认] 当上层应用App调用recv系统调用时,如果对等方没有发送数据(Linux内核缓冲区中没有数据),上层应用Application1将阻塞;当对等方发送了数 ...