Problem:给两个正数分别为n和k,求出从1,2.......n这n个数字选择k个数字作为一个组合,求出所有的组合。
 
数学问题:排列组合问题,可以得到这样的组合个数为:C(n,k)
 
 
代码实现:递归程序实现。
从1开始遍历到n为止,中间使用tempList保存每一个组合,只有当这个tempList的长度等于k时,将这个tempList添加到ans中。
 
因此该递归程序的出口为:保存暂时组合的数组长度等于k,
递归主要内容为向tempList中添加数字,调用自身,弹出tempList顶层的元素。
 
参考代码:
package leetcode_100;

import java.util.ArrayList;
import java.util.List; public class Solution77 { public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> ans = new ArrayList<List<Integer>>();
match(ans,new ArrayList<Integer>(),1,n,k);
return ans;
} private void match(List<List<Integer>> ans, ArrayList<Integer> arrayList, int start, int n, int k) {
if(k==0){
ans.add(new ArrayList<Integer>(arrayList));
return;
}
for(int i = start ; i <= n ; i++){
arrayList.add(i);
match(ans,arrayList,i+1,n,k-1);
arrayList.remove(arrayList.size()-1);
}
} }

LeetCode 77 Combinations(排列组合)的更多相关文章

  1. [LeetCode] 77. Combinations 全组合

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

  2. leetCode 77.Combinations (组合)

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

  3. [leetcode]77. 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. 数组排列组合问题——BACKTRACKING

    BACKTRACKING backtracking(回溯法)是一类递归算法,通常用于解决某类问题:要求找出答案空间中符合某种特定要求的答案,比如eight queens puzzle(将国际象棋的八个 ...

  6. LeetCode OJ:Combinations (排列组合)

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

  7. [leetcode] 题型整理之排列组合

    一般用dfs来做 最简单的一种: 17. Letter Combinations of a Phone Number Given a digit string, return all possible ...

  8. LeetCode 77,组合挑战,你能想出不用递归的解法吗?

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode第46篇文章,我们一起来LeetCode中的77题,Combinations(组合). 这个题目可以说是很精辟了,仅仅 ...

  9. 【LeetCode每天一题】Permutations(排列组合)

    Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...

随机推荐

  1. iOS:当点击 FormSheet 之外时,关闭该视图

    @interface XXViewController (){ @property (strong, nonatomic) UITapGestureRecognizer *tapGesture; - ...

  2. 5.5 进入编辑模式 5.6 vim命令模式 5.7 vim实践

    5.5 进入编辑模式 5.6 vim命令模式 5.7 vim实践 进入编辑模式 小写i在当前字符前插入 大写I 在光标所在行的行首插入 大写O 在光标上面一行插入编辑 小写o在光标下面一行插入编辑 小 ...

  3. Fragment管理工具类

    Fragment相关→FragmentUtils.java→Demo addFragment : 新增fragment removeFragment : 移除fragment replaceFragm ...

  4. 2 python大数据挖掘系列之淘宝商城数据预处理实战

    preface 在上一章节我们聊了python大数据分析的基本模块,下面就说说2个项目吧,第一个是进行淘宝商品数据的挖掘,第二个是进行文本相似度匹配.好了,废话不多说,赶紧上车. 淘宝商品数据挖掘 数 ...

  5. maven package 与maven install的区别

    maven package:会将jar包打包到target下 maven install:将jar包装载到maven仓库,供其他项目使用 项目基于osgi开发的,打包有依赖关系,依赖关系主要是在pom ...

  6. linux中如何通过echo输出!(叹号)? -bash: !": event not found

    需求描述: 今天在做通过echo结合passwd给用户改密码的过程中,出现无法修改的错误. 错误如下: [root@testvm ~]# useradd mytest [root@testvm ~]# ...

  7. 【scala】 scala 基础(一)

    至于什么是scala,摘录一段 维基百科的解释: scala 下载 安装 省略 1.环境变量配置完成后 命令行报错,因为scala 的安装路径里边包含空格 修改后即可.由于我的本地包含空格,此处CLI ...

  8. SPOJ QTREE5 lct

    题目链接 对于每一个节点,记录这个节点所在链的信息: ls:(链的上端点)距离链内部近期的白点距离 rs:(链的下端点)距离链内部近期的白点距离 注意以上都是实边 虚边的信息用一个set维护. set ...

  9. 如何在O(n)的时间复杂度内找出数组中出现次数超过了一半的数

    方法一:每次取出两个不同的数,剩下的数字中重复出现次数超过一半的数字肯定,将规模缩小化.如果每次删除两个不同的数,这里当然不是真的把它们踢出数组,而是对于候选数来说,出现次数减一,对于其他数来说,循环 ...

  10. ubuntu11.10 64bit 编译android 4.0

    前言: 据说google内部使用的的ubuntu版本始终是10.4, 而我一直使用的编译2.3Gingerbread的 11.04下补充安装uuid-dev和liblzo2-dev两个库就可以直接编译 ...