[LeetCode]77 组合

2018-11-04

题目描述

给定两个整数 n 和 k,返回 1 … n 中所有可能的 k 个数的组合。

示例:

输入: n = 4, k = 2
输出:
[
  [2,4],
  [3,4],
  [2,3],
  [1,2],
  [1,3],
  [1,4],
]

代码

1
2
3
4
5
6
7
8
class Solution:
def combine(self, n, k):
"""
:type n: int
:type k: int
:rtype: List[List[int]]
"""
return list(itertools.combinations(range(1, n+1), k))
分享
  • 回溯算法
前一篇
[LeetCode]78 子集
下一篇
[LeetCode]76 最小覆盖子串
  • © 2019 Ethan Woo
  • Powered by Hexo
Ethan Woo
  • Home
  • Archives
  • Gallery
  • About
  • Search