-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode_77.py
More file actions
39 lines (33 loc) · 814 Bytes
/
leetcode_77.py
File metadata and controls
39 lines (33 loc) · 814 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# coding=utf-8
from typing import List
class Solution:
"""
数字组合
"""
def combine(self, n: int, k: int, start: int, elem: List[int], ret: List[List[int]]):
"""
:param n:
:param k:
:param start:
:param elem:
:param ret:
:return:
"""
if k == 0:
ret.append(list(elem))
return
for i in range(start, n - k + 2):
elem.append(i)
self.combine(n, k - 1, i + 1, elem, ret)
elem.pop()
def combine_recursive(self, n: int, k: int) -> List[List[int]]:
"""
:param n:
:param k:
:return:
"""
if k > n:
return []
ret, elem = [], []
self.combine(n, k, 1, elem, ret)
return ret