We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 013c616 commit 8f6e958Copy full SHA for 8f6e958
Combinations/Combinations.py
@@ -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
24
25
26
27
28
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