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

Example:

Input: n = 4, k = 2
Output:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
] 思路

  这道题和之前的做的排列组合很相似,一个是数组中所有数字进行组合,这个是规定组合个数并且相同的数字算相同的组合,因此我们可以在排列组合的代码上进行改进。就可以得到答案。详见代码。
解决代码


 class Solution(object):
def combine(self, n, k):
"""
:type n: int
:type k: int
:rtype: List[List[int]]
"""
list_nums = list(range(1, n+1)) # 先构建一个包含n个数字的数组
res = []
self.permution(list_nums, k, [], res) # 进行组合
return res def permution(self, nums, k, path, res):
if len(path) == k: # 当path中个数等于k的时候,表示得到了一种组合
res.append(path)
return
for i in range(len(nums)): # 从数组第一个元素开始进行组合
self.permution(nums[i+1:], k, path+[nums[i]], res)

【LeetCode每天一题】Combinations(组合)的更多相关文章

  1. leetcode第40题:组合总和II

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...

  2. leetcode第39题:组合综合

    给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选 ...

  3. [FollowUp] Combinations 组合项

    这是Combinations 组合项 的延伸,在这里,我们允许不同的顺序出现,那么新的题目要求如下: Given two integers n and k, return all possible c ...

  4. 【JavaScript】Leetcode每日一题-组合总和4

    [JavaScript]Leetcode每日一题-组合总和4 [题目描述] 给你一个由 不同 整数组成的数组 nums ,和一个目标整数 target .请你从 nums 中找出并返回总和为 targ ...

  5. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  6. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  7. LeetCode第[1]题(Java):Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  8. combinations(组合)

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

  9. LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  10. [LeetCode] 377. Combination Sum IV 组合之和 IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

随机推荐

  1. Qt学习记录--02 Qt的信号槽机制介绍(含Qt5与Qt4的差异对比)

    一 闲谈: 熟悉Window下编程的小伙伴们,对其消息机制并不陌生, 话说:一切皆消息.它可以很方便实现不同窗体之间的通信,然而MFC库将很多底层的消息都屏蔽了,尽管使用户更加方便.简易地处理消息,但 ...

  2. Android Studio Gradle Build Running 加速

    加速效果

  3. Web重温系列(一):利用寄宿于IIS的WCF序列化文件

    这两年一直在做WinForm,对于Web已经比较生疏了,其实之前做的也不是很多. 这两天做了一个小工具,功能很简单,就是想有个地方存放办公室同事的机器名和IP的信息,包括附加的用户名和更新时间.比较之 ...

  4. tensorflow简单记录summary方法

    虽然tf官方希望用户把 train , val 程序分开写,但实际开发中,明显写在一起比较简单舒服,但在保存数据到 summary 时, val 部分和 train 部分不太一样,会有一些问题,下面讨 ...

  5. 基于【CentOS-7+ Ambari 2.7.0 + HDP 3.0】HAWQ数据仓库 使用之 gpfdist协议

    一.HAWQ基本安装自带gpfdist协议 gpfdist是HAWQ支持的外部表访问协议之一,这是hawq自带的一个简单的集成http服务命令. 在我的前述安装hawq之后,gpfdist命令位于ha ...

  6. [原]Jenkins(二十一) jenkins再出发Build periodically和Poll SCM

    缘由: 使用jenkins的目的需要固定时间构建和间隔固定时间构建,所以才会用到这两个功能. 位置:这两个功能的位置位于每个job的config项中,如下图: [重要的注意点:]           ...

  7. TTL是什么意思?

    TTL是一个ip协议的值,它告诉网络,数据包在网络中的时间是否太长而应被丢弃.有很多原因使包在一定时间内不能被传递到目的地. TTL 的初值通常是系统缺省值,是包头中的8位的域.TTL的最初设想是确定 ...

  8. git使用——推送本地文件到远程仓库

    捣鼓了一下午之后总结如下:   1.首先可以照着这个链接里面博主给出的详细方法进行操作和配置: http://www.open-open.com/lib/view/open1454507333214. ...

  9. Bootstrap模态框原理分析及问题解决

    最近自学了bootstrap觉得里面模板样式挺好的,就想自己实现实现,不多说了,开始进入正题了 今天就来实现bootstrap里面的模态框弹出效果 首先很简单 实现一个类似于panel的modal 1 ...

  10. common lisp里的几个操作符

    setf  赋值操作符,定义一个全局变量.返回值是最后一个赋值的结果. let 局部变量操作符.let表达式有两部分组成.第一部分是任意多的变量赋值,他们被包裹在一个()中,第二部分是任意数量的表示式 ...