Leetcode 77, Combinations
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的更多相关文章
- LeetCode 77 Combinations(排列组合)
题目链接:https://leetcode.com/problems/combinations/#/description Problem:给两个正数分别为n和k,求出从1,2.......n这 ...
- [LeetCode] 77. Combinations 全组合
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- [leetcode]77. Combinations组合
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...
- leetCode 77.Combinations (组合)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- 【一天一道LeetCode】#77. Combinations
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- 【LeetCode】77. Combinations 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- LeetCode OJ 77. Combinations
题目 Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exa ...
- 【LeetCode】77. Combinations (2 solutions)
Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ... ...
- LeetCode 77. 组合(Combinations)
题目描述 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], ...
随机推荐
- Github 简明教程
http://www.runoob.com/w3cnote/git-guide.html http://rogerdudler.github.io/git-guide/index.zh.html
- VS2010 生成Xml格式的注释文档
项目, 属性, build, 勾选xml document file, 重新build, 即可生成xml注释文件, 然后还得找工具软件(看到anytao推荐SandCastle) 生成更易读的帮助文档 ...
- c语言 指针与地址的区别
指针由两部分组成,指针的类型和指针的值(也就是变量的地址). 指针和地址的区别: 地址只是一堆十六进制的字符,对应着内存条的某段内存, 而指针本身有地址,指针的值也是一个地址,指针本身还有类型,这与单 ...
- 数据字典生成工具之旅(7):NVelocity实现代码生成器
这个系统好久没有更新了,人也慢慢变懒了,从现在开始每个月至少写三篇文章,欢迎大家监督.对了预告一下,该系列完成以后将为大家带来WebApp开发系列篇,敬请期待.先上几张图,放在文章最后面欢迎预览! 本 ...
- windows7下启动mysql服务出现服务名无效
出现提示: WIN 7 cmd命令行下,net start mysql,出现 服务名无效提示: 问题原因: mysql服务没有安装. 解决办法: 在 mysql bin目录下 以管理员的权限 执行 m ...
- PRML读书会第九章 Mixture Models and EM(Kmeans,混合高斯模型,Expectation Maximization)
主讲人 网络上的尼采 (新浪微博: @Nietzsche_复杂网络机器学习) 网络上的尼采(813394698) 9:10:56 今天的主要内容有k-means.混合高斯模型. EM算法.对于k-me ...
- WPF 3D模型 3D场景
1.首先得说明的是这并不是真正的3D,模型被导出为一系列的单个图片,例如一个3D户型图,以某个视角旋转360°,渲染出一系列连续的单个图片文件. 2.在Image.MouseMove事件中添加相应代码 ...
- C#中数组Array、ArrayList、泛型List<T>的比较
在C#中数组Array,ArrayList,泛型List都能够存储一组对象,但是在开发中根本不知道用哪个性能最高,下面我们慢慢分析分析. 一.数组Array 数组是一个存储相同类型元素的固定大小的顺序 ...
- [转]linux 系统监控、诊断工具之 IO wait
1.问题: 最近在做日志的实时同步,上线之前是做过单份线上日志压力测试的,消息队列和客户端.本机都没问题,但是没想到上了第二份日志之后,问题来了: 集群中的某台机器 top 看到负载巨高,集群中的机器 ...
- android服务之MP3播放(2)
该播放器将会直接从网络上获取资源进行播放,并提供进度条显示的功能 布局文件 布局文件中使用Seekbar组件来显示进度条 <?xml version="1.0" encodi ...