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. vim定位到指定行数

    显示行号:命令模式下set nu 定位到指定行: 命令模式下,:n   比如想到第2行,:2 编辑模式下,ngg  比如想到第5行 5gg(或者5G) 打开文件定位到指定行   vim  +n  te ...

  2. Solaris10 修改hostname

    修改/etc/nodename 及 /etc/hosts & /etc/hostname.<NIC Name> 这三个文件就可以了 重启

  3. Python爬虫学习——获取网页

    通过GET请求获取返回的网页,其中加入了User-agent信息,不然会抛出"HTTP Error 403: Forbidden"异常, 因为有些网站为了防止这种没有User-ag ...

  4. 【WP8】MultiBinding

    WP中系统没有像WPF中直接支持MultiBinding,可以通过以下代码实现 五个类 public class BindingCollection : Collection<BindingBa ...

  5. 如果返回结构体类型变量(named return value optimisation,NRVO) ------ 续

    为什么? <More C++ idioms>: 3. Algebraic Hierarchy 程序执行的流程与自己想的不一样: Number Number::makeReal(double ...

  6. srv.exe蠕虫病毒~

    你是否在电脑使用过程中遇到过这样的问题: 1.文件运行后,同目录下会出现一个原名 srv.exe的文件 2.文件运行后会把浏览器打开 3.电脑上的html文件末尾会增加一大堆东西 完了,电脑中了srv ...

  7. 使用T4模板调用Sqlserver链接生成自己的模板

    <#@ template debug="false" hostspecific="false" language="C#" #> ...

  8. [原]unity3d之http多线程异步资源下载

    郑重声明:转载请注明出处 U_探索 本文诞生于乐元素面试过程,被面试官问到AssetBundle多线程异步下载时,愣了半天,同样也被深深的鄙视一回(做了3年多u3d 这个都没用过),所以发誓要实现出来 ...

  9. mysql触发器的使用 想让b字段在更新的时候把旧数据保存到a字段中

    使用mysql希望数据库自动触发一些规则,进行更新数据的时候,就需要用触发器了,比如 将旧数据保存到额外字段中,如何做呢? 在abc表中 name更新的时候 我希望把name的老数据保存到 old_n ...

  10. Django 配置

    Django 配置   运行 django-admin.py startproject [project-name] 命令会生成一系列文件,在Django 1.6版本以后的 settings.py 文 ...