Skip to content

Commit 8f6e958

Browse files
Add files via upload
1 parent 013c616 commit 8f6e958

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Combinations/Combinations.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# 第一种思路
2+
# 740ms 45.65%
3+
class Solution:
4+
def combine(self, n, k):
5+
"""
6+
:type n: int
7+
:type k: int
8+
:rtype: List[List[int]]
9+
"""
10+
def mycombine(num_list, temp_list):
11+
if len(temp_list) == k - 1:
12+
for num in num_list:
13+
res_list.append(temp_list + [num])
14+
for index in range(len(num_list)):
15+
mycombine(num_list[index + 1:], temp_list + [num_list[index]])
16+
17+
res_list = []
18+
mycombine(list(range(1, n + 1)), [])
19+
return res_list
20+
21+
# 第二种思路,讨论区一个大神给出的思路,但是并没有看懂
22+
# 172ms 76.24%
23+
class Solution:
24+
def combine(self, n, k):
25+
"""
26+
:type n: int
27+
:type k: int
28+
:rtype: List[List[int]]
29+
"""
30+
if k == 0:
31+
return [[]]
32+
return [pre + [i] for i in range(k, n+1) for pre in self.combine(i-1, k-1)]

0 commit comments

Comments
 (0)