Combinations

题意:

  根据给定的n和k,生成从1到n范围内长度为k的排列组合

示例:

  n=4 k=2

[[1, 2],
[1, 3],
[1, 4],
[2, 1],
[2, 3],
[2, 4],
[3, 1],
[3, 2],
[3, 4],
[4, 1],
[4, 2],
[4, 3]]

解题:

  正常情况下我们通常想到的都是通过使用递归,以枚举的形式来生成组合。先从给定的范围中拿一个数出来,把它同剩下的每一个数进行组合,然后再在每个组合上对不存在于组合的每个数进行合并,这样依次的进行合并操作,最终生成我们需要的排列。

  但我发现,使用这个方法在leetcode上提交是会提示超时的。后来我发现其实我们可以使用排列组合中的公式:

C(n,k) = C(n-1,k) + C(n-1, k-1)

  这个公式直观的解释是,求范围n中长度为k的排列组合个数可以换算成求范围n-1中长度为k的排列组合个数与范围n-1中长度为k-1的排列组合个数之和。

  这么听起来,不仅绕口,而且感觉貌似这个公式和我们要达到的目的貌似并没有什么用。。其实我们换一种角度就很容易理解了。

  我们要求范围n长度为k的排列组合,如果我们可以求得范围n-1长度k的排列组合,那么我们还需要求出包含数字n的长度为k的排列组合,然而求范围n-1长度为k-1

的排列组合正好就是为了并上数字n,这样获得的排列组合正好是求出C(n-1,k)所差得哪些排列。因此我们的代码可以写成:

class Solution(object):
def combine(self, n, k):
"""
:type n: int
:type k: int
:rtype: List[List[int]]
"""
if n == k:
return [range(1, n+1)]
if k == 1:
return [[item] for item in range(1, n+1)]
ret = self.combine(n-1, k)
other = self.combine(n-1, k-1)
for item in other:
item.append(n)
ret += other
return ret

   如果要想我们的code 显得更加的pythonic,我们还可以这样写:

class Solution(object):
def combine(self, n, k):
"""
:type n: int
:type k: int
:rtype: List[List[int]]
"""
if n == k:
return [range(1, n+1)]
if k == 1:
return [[item] for item in range(1, n+1)]
return self.combine(n-1, k) + [item + [n] for item in self.combine(n-1, k-1)]

leetcode-Combinations 复习复习排列组合的更多相关文章

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

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

  2. leetCode 47.Permutations II (排列组合II) 解题思路和方法

    Permutations II  Given a collection of numbers that might contain duplicates, return all possible un ...

  3. LeetCode 77 Combinations(排列组合)

    题目链接:https://leetcode.com/problems/combinations/#/description    Problem:给两个正数分别为n和k,求出从1,2.......n这 ...

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

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

  5. [LeetCode] Combinations 组合项

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

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

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

  7. leetcode — combinations

    import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Source : https://o ...

  8. 【Python】排列组合itertools & 集合set

    ■itertools 利用python的itertools可以轻松地进行排列组合运算 itertools的方法基本上都返回迭代器 比如 •itertools.combinations('abcd',2 ...

  9. 用js实现排列组合

    在leetcode上看到一个题,代码实现排列组合的. 记得大学上课时候,就用c写过,现在用js试试,顺便看看耗时. 先看看3的阶乘: function permute(temArr,testArr){ ...

随机推荐

  1. MyGame--java语言编写的打飞机游戏(附源码下载)

    运行效果如下图所示: 点击这里进行下载, 还有源码已经传至我的github上,还有一些小bug,欢迎大家改正. 说明:最后打boss的效果还没做,爆炸的图片也没好,欢迎大家修改.

  2. JavaScript学习笔记-正则表达式(RegExp对象)

    正则表达式(RegExp对象)   1.正则表达式字面量,在脚本加载后编译.若你的正则表达式是常量,使用这种方式可以获得更好的性能,重复使用时不会重新编译: 2.使用构造函数创建的RegExp,提供了 ...

  3. mvc model 传值两种方式区别

    1: controller中: public actionresult index() { M m=new M(); return view(m) } view中: @model.phone vs 中 ...

  4. webstorm常用的快捷键总结

    1. ctrl + shift + n: 打开工程中的文件,目的是打开当前工程下任意目录的文件. 2. ctrl + j: 输出模板 3. ctrl + b: 跳到变量申明处 4. ctrl + al ...

  5. URL 路径长度限制(错误:指定的文件或文件夹名称太长)

    本节讨论 URL 的构成.SharePoint 2010 构建 URL 的方式.URL 的编码和加长以及作为其他 URL 中的参数传递的方式. SharePoint URL 的构成 SharePoin ...

  6. C# 获得MP4时长

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  7. linux heartbeat v2/v3 的一点资料

    http://linux-ha.org http://linux-ha.org/wiki/Pacemaker Heartbeat2 http://blog.taggesell.de/index.php ...

  8. 在Json解析过程中,我为什么用object1.optInt ,和 object1.optString

    今天在做Json解析的时候,出现了一段代码没执行的问题,于是找了一下原因: 1.原代码是:   发现 红色的一句 没有执行,查看控制台发现了异常 2.修复bug ,正确的代码为        3.总结 ...

  9. 1.3 基础知识——GP2.1 方针(Policy)

    摘要: 方针这个GP每个PA都有,其实CMMI实践有没有实在价值,就在于方针!如果我们做出来的CMMI实践仅仅就是写文档.多步骤.没事找事,那其实就是违背了公司的商业目标,公司的商业目标简单说就是:用 ...

  10. jQuery添加options点击事件并传值

    说明: 根据选择不同店铺选项,上送不同id值,展示不同商品列表   var formStr = "{'supplierId':'供应链企业|%-jm-sprt-%|93794498-3'}& ...