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# 连接mysql配置config,不用装net connector

    <system.data> <DbProviderFactories> <remove invariant="MySql.Data.MySqlClient&qu ...

  2. Godray

    软管的这个有点蛋疼..应该是我材质没弄好 最后发现不是材质,是法线不正确,调整后

  3. u3d资源打包只能打包场景材质,不能打包脚本

    GameObject附带脚本打包进来以后,只有一个脚本名字,估计只是关联了脚本,内容却不在,所以后面打包资源的话,一定要把脚本,shader之类的,放到工程目录下

  4. u3d fpsCounter

    因为u3d自己的stats下面的fpscounter不是实际意义上的fps,所以看到demo的fpsCounter,把它改写为c#的 using UnityEngine;using System.Co ...

  5. ZeroClipboard插件——复制到剪切板

    ZeroClipboard是一个轻量级的jQuery“复制到剪贴板”插件采用了时下流行的零剪贴板库.官网:http://www.steamdev.com/zclip 参数及默认值path(必选)  Z ...

  6. win8 关闭防火墙

    http://jingyan.baidu.com/article/b87fe19eddb4da5218356894.html

  7. 在Unity中查找缺失的引用

    这篇博客是查找unity中缺失引用的一个简单简短的解决方案.你可以从GitHub上获取源码. 缺失引用 一个丢失引用与没有引用(在检视表显示“None”)是完全不同的概念.这些友各种原因造成,比如:把 ...

  8. python使用tkinter写带界面的工具

    python一般用来写纯脚本的居多,但也可以做有视图的产品出来,例如做网页和客户端工具.做成工具的好处是,让不懂代码的人也能使用,不需要去修改代码里面的参数,如果使用次数频繁,甚至比纯脚本跟节约时间: ...

  9. Java实现策略模式的简单应用

    在使用图像处理软件处理图片后,需要选择一种格式进行保存.然而各种格式在底层实现的算法并不相同,这刚好适合策略模式.编写程序,演示如何使用策略模式与简单工厂模式组合进行开发. 思路如下: 使用inter ...

  10. Apache双机热备

    部署方案 1.1 方案设计 1.2 方案描述 如上图所示,我们要有三个可用的IP地址(切记不能与网络中其他机器IP重复),针对我使用的三个IP地址做如下说明: 10.16.252.10 //这个IP地 ...