Leetcode: Combinations
2015-09-09 06:04
344 查看
Question
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],
]
Show Tags
Show Similar Problems
Solution
Get idea from here.In the recursion function, it is efficient to prun.
[code]class Solution(object): def combine(self, n, k): """ :type n: int :type k: int :rtype: List[List[int]] """ res, cur = [], [] if n<0 and n<k: return res self.helper(n, k, 1, cur, res) return res def helper(self, n, k, start, cur, res): if len(cur)==k: res.append(cur) return for elem in range(start,n+1): self.helper(n, k, elem+1, cur+[elem], res)
相关文章推荐
- jQuery满屏焦点图左右滚动特效代码分享
- 纯javascript代码实现计算器功能(三种方法)
- js实现全国省份城市级联下拉菜单效果代码
- 原生JS实现美图瀑布流布局赏析
- js实现可折叠展开的手风琴菜单效果
- js调用百度地图及调用百度地图的搜索功能
- jQuery平滑旋转幻灯片特效代码分享
- jquery实现简单的二级导航下拉菜单效果
- js+css实现超简洁的二级下拉菜单效果代码
- 关于JS中prototype的理解
- jquery实现标题字体变换的滑动门菜单效果
- js实现兼容性好的微软官网导航下拉菜单效果
- JS实现弹性菜单效果代码
- jQuery实现简单下拉导航效果
- jquery的幻灯片图片切换效果代码分享
- JavaScript简单下拉菜单实例代码
- jquery仅用6行代码实现滑动门效果
- jquery实现通用的内容渐显Tab选项卡效果
- 基于jQuery全屏焦点图左右切换插件responsiveslides
- js图片轮播特效代码分享