combinations(组合)
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(组合)的更多相关文章
- [FollowUp] Combinations 组合项
这是Combinations 组合项 的延伸,在这里,我们允许不同的顺序出现,那么新的题目要求如下: Given two integers n and k, return all possible c ...
- [LeetCode] Combinations 组合项
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- 【LeetCode每天一题】Combinations(组合)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...
- leetCode 77.Combinations (组合)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- [Leetcode] combinations 组合
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- 077 Combinations 组合
给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合.例如,如果 n = 4 和 k = 2,组合如下:[ [2,4], [3,4], [2,3], [1,2], [ ...
- [leetcode]77. Combinations组合
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...
- Leetcode77. Combinations组合
给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], [1,3] ...
- Python排列组合问题
1.字符串的全排列 问题描述:打印出原字符串中所有字符的所有排列.——将输入字符串中的每个字符作为一个不同的字符看待,即使它们是重复的,如'aaa'应打印6次. Python可以用生成器解决: def ...
随机推荐
- Android开发学习之路--Android系统架构初探
环境搭建好了,最简单的app也运行过了,那么app到底是怎么运行在手机上的,手机又到底怎么能运行这些应用,一堆的电子元器件最后可以运行这么美妙的界面,在此还是需要好好研究研究.这里从芯片及硬件模块-& ...
- jquery 只读
大家都理解这是什么,正常的写法如下: if (status == true) { $("#minDelistStr").val(totalAmount);// 去掉首部的" ...
- web安全认证机制知多少
如今web服务随处可见,成千上万的web程序被部署到公网上供用户访问,有些系统只针对指定用户开放,属于安全级别较高的web应用,他们需要有一种认证机制以保护系统资源的安全,本文将探讨五种常用的认证机制 ...
- javascript之DOM编程根据属性找标签练习
首先看一下需求: 当点击全选时,选中所有的,当再点击时,全部取消.且选中某些项,点击总金额,会显示处总的金钱数. <!DOCTYPE html PUBLIC "-//W3C//DTD ...
- spring4泛型初探----一个小例子
泛型的出现,是为了让代码更规整. 例如 Set<String> set=new HashSet<>(); set.add("abc"); set.add(1 ...
- Linux 学习笔记_12_文件共享服务_2_FTP应用--vsftpd
Wu-FTP:古老,配置复杂 Proftp:功能强大 vsftp: 安全,高速,稳定[系统默认的FTP软件] VSFTP服务器配置 启动:/etc/rc.d/init.d/vsftpd start [ ...
- 高性能C++网络库libtnet实现:http
HTTP libtnet提供了简单的http支持,使用也很简单. 一个简单的http server: void onHandler(const HttpConnectionPtr_t& con ...
- 【Python】Talk Python To Me Podcast播客
这是Python相关的一个播客,通过播客的形式给大家讲述python那点事,相关的链接都会列出来,有一些是由文本内容的,如果听不太懂就看看英文原文.不fanqiang的情况下,网页打开没有问题,但是播 ...
- View,ViewGroup的Touch事件的分发机制
原帖地址:http://blog.csdn.net/xiaanming/article/details/21696315 ViewGroup的事件分发机制 我们用手指去触摸Android手机屏幕,就会 ...
- Python进阶 函数式编程和面向对象编程等
函数式编程 函数:function 函数式:functional,一种编程范式.函数式编程是一种抽象计算机的编程模式. 函数!= 函数式(如计算!=计算机) 如下是不同语言的抽象 层次不同 高阶函数: ...