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. 编写高质量代码_改善C++程序的150个建议 读书笔记

    这几天看了下这本书<编写高质量代码_改善C++程序的150个建议>,觉的蛮有收获的,再次记录下自己以前不清晰的知识点,以供学习. 编写符合标准的main函数 C语言标准规定了main函数的 ...

  2. maven package 与maven install的区别

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

  3. Bootstrap——导航居中

    这是采用了栅格,设置缩进,使看起来居中,但是手机浏览会靠到最左边.另外center-block类好像也不管用. <div class="row"> <ul cla ...

  4. Linux Shell 通配符、元字符、转义符使用实例介绍

    https://www.cnblogs.com/chengmo/archive/2010/10/17/1853344.html

  5. Nginx服务器之负载均衡策略

    http://www.cnblogs.com/1214804270hacker/p/9325150.html

  6. 关于修改linux hostname的问题,尤其是redhat 7修改hostname的方式

    http://blog.csdn.net/the_conquer_zzy/article/details/68064149

  7. node.js--Less

    摘要: 现在已经有许多站点使用Node.js,所以在Node.js上配置Less环境也是很重要的,下面分享下如何在Node上使用Less开发,前提是你电脑上已经安装node. 安装: 只需要执行下面一 ...

  8. WebBrowser中html元素如何触发winform事件

    这个问题来自论坛提问,对dom稍微了解的话还是比较简单的,只要注册一下事件就可以了. C#代码如下: using System;using System.ComponentModel;using Sy ...

  9. MySQL 安装与配置

    Linux 安装 MySQL Windows 安装 MySQL 如何连接 MySQL 如何修改 MySQL 密码 如何重置 MySQL 密码

  10. VS调试DLL代码使用”附加到进程“

    如果一个DLL解决方案,被另一个DLL2解决方案依赖,DLL2被可执行程序exe1引用 如何调试DLL的代码断点呢 1.可以参考另一篇随笔DLL如何调试 2.先运行起来exe1,然后再DLL项目中”调 ...