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. 浅析正则表达式模式匹配的String方法

    在JavaScript代码中使用正则表达式进行模式匹配经常会用到String对象和RegExp对象的一些方法,例如replace.match.search等方法,以下是对一些方法使用的总结. Stri ...

  2. 看代码学知识之(2) ListView无数据时显示其他View

    看代码学知识之(2) ListView无数据时显示其他View 今天看的一块布局是这样的: <!-- The frame layout is here since we will be show ...

  3. Android自定义控件2--优酷菜单界面初始化

    本文开始将逐步去实现下面优酷菜单的效果: 本文地址:http://www.cnblogs.com/wuyudong/p/5912538.html,转载请注明源地址. 本文首先来实现优酷菜单界面初始化工 ...

  4. 你真的了解UITableViewCell重用吗?

    一:首先查看一下关于UITableViewCell重用的定义 - (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentif ...

  5. 网络婚礼之AFNetWorking3.0

    目前使用人数最多的第三方网络库,没有之一.从开始的NSURLConnection到现在的NSURLSession,它都一直保持着与苹果的步调一致,而由它也衍生出大量的相关第三方网络功能库,不仅仅因为他 ...

  6. iOS开发之UISearchBar初探

    iOS开发之UISearchBar初探 UISearchBar也是iOS开发常用控件之一,点进去看看里面的属性barStyle.text.placeholder等等.但是这些属性显然不足矣满足我们的开 ...

  7. sqlserver 附加数据库失败,错误提示:5拒绝访问 解决办法

    sqlserver 附加数据库失败,错误提示:5拒绝访问 解决办法 金刚 sqlserver 附加数据库 拒绝访问 今天把项目拷贝到新硬盘里,发现在附加数据库中提示:操作系统错误5:"5拒绝 ...

  8. Listener监听器与Filter过滤器

    1.Listener     [1]监听器简介         > Listener是JavaWeb的三大组件之一,Servlet.Filter.Listener         > Li ...

  9. png-8 和 png-24的区别

    png是一种图片格式,是Portable Networks Graphics的缩写,做ping. png8和png24的区别如下. 1 "PNG8"是指8位索引色位图," ...

  10. List 简单升\降序实现

    public class User { public int Id { get; set; } public string Code { get; set; } } //示例 //升序 list.So ...