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

For example,
If n = 4 and k = 2, a solution is:

[
[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]]
"""
res = []
nums = list(range(1, n+1))
self.dfs(nums, k, 0, res, []) return res def dfs(self, nums, k, step, res, line):
if len(nums) < k - step:
return
if step == k:
res.append([x for x in line]) for i, x in enumerate(nums):
line.append(nums[i])
self.dfs(nums[i+1:], k, step + 1, res, line)
line.pop()

L15-L16  判段一下剩下的可以填的元素个数是不是比剩下的空位少,如果是的话可以提前结束这个branch的搜索。没有这两行的话,程序超时。L22说明,如果去了一个num之后,在line里面这个num之后的数只取比它大的数,这样就保证了最后结果中不会出现重复的情况,比如在答案中只可能出现[1,2,3],而不可能出现[2,1,3]。

Leetcode 77, Combinations的更多相关文章

  1. LeetCode 77 Combinations(排列组合)

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

  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. 【一天一道LeetCode】#77. Combinations

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  6. 【LeetCode】77. Combinations 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  7. LeetCode OJ 77. Combinations

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

  8. 【LeetCode】77. Combinations (2 solutions)

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

  9. LeetCode 77. 组合(Combinations)

    题目描述 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], ...

随机推荐

  1. ubuntu下crontab编辑方法的设定

    在ubuntu下,首次编辑crontab计划任务的时候,会提示让选择编辑器.由于对nano编辑器不是很熟悉,若是选择nova编辑的话,会有些麻烦.可以重置编辑器,方法如下:[root@wang ~]# ...

  2. Nginx/Apache服务连接数梳理

    统计连接数,使用netstat命令或ss命令都可以1)统计连接数(80端口)[root@wang ~]# netstat -nat|grep -i "80"|wc -l872 或者 ...

  3. 2016.11.14测试 长乐一中2014NOIP复赛模拟题 第一题。

    1.正确答案 [题目描述] 小H与小Y刚刚参加完UOIP外卡组的初赛,就迫不及待的跑出考场对答案. "吔,我的答案和你都不一样!",小Y说道,"我们去找神犇们问答案吧&q ...

  4. Linux 信号详解三(sleep,raise)

    sleep()函数 .sleep()函数作用:让进程睡眠 .能被信号打断,然后处理信号函数以后,就不再睡眠,直接向下执行代码 .sleep函数的返回值是剩余秒数 //sleep 函数 #include ...

  5. mybatis 3.2.8 + log4j2.0.2 控制台输出sql语句

    mybatis3.2.7有一个bug,使用log4j2 (2.0.2)版本时,会找不到类 ,导致启动失败,详见 https://github.com/mybatis/mybatis-3/issues/ ...

  6. 前端见微知著番外篇:Bitbucket进行代码管控

    说道代码管控,一般都会提到TFS.Git等,但是在这里我们将要用到Bitbucket,其实其操作方式和Git基本上一样,但是和TFS则有很大的不同了.但是原理基本上都是一致的. 这里我不会过多的涉及到 ...

  7. 使用EasyUI布局时出现混乱瞬间的解决方法

    在所有form代码之前加遮罩层 <div id='PageLoadingTip' style="position: absolute; z-index: 1000; top: 0px; ...

  8. android开发------编写用户界面之线性布局

    一个好的应用程序离不开人性化的用户界面.在学习其他东西之前.理应先学习编写程序的布局(外观) 今天,我们就来学习android的UI布局----LinearLayout. LinearLayout,即 ...

  9. Shell脚本编程中的几个问题

    条件语句 正确的写法: if [ $1 = "-f" ] #注意这里,前后方括号和中间的内容之间必须有空格! then commands fi 错误的写法: if [$1 == & ...

  10. C++折半插入排序

    代码如下: #include <iostream> using namespace std; void insertSort(int a[], int n) { for(int i=1;i ...