File tree Expand file tree Collapse file tree 1 file changed +12
-17
lines changed Expand file tree Collapse file tree 1 file changed +12
-17
lines changed Original file line number Diff line number Diff line change @@ -31,26 +31,21 @@ Constraints:
3131
3232``` python
3333def combine (self , n : int , k : int ) -> List[List[int ]]:
34- def gen_pattern ( nums , k ):
35- if k == 0 :
36- return [[]]
37- if k == 1 :
38- return [[num] for num in nums]
34+ result = []
35+ def gen_pattern ( build , index ) :
36+ if len (build) == k:
37+ result.append(build)
38+ return
3939
40- res = []
41- for i in range (len (nums)):
42- perms = gen_pattern(nums[i+ 1 :], k- 1 )
43- for p in perms:
44- res.append([nums[i]] + p)
45- return res
46-
47- nums = list (range (1 , n+ 1 ))
48- res = gen_pattern(nums, k)
49- return res
40+ for i in range (index+ 1 , n+ 1 ):
41+ gen_pattern(build+ [i], i)
42+
43+ gen_pattern([], 0 )
44+ return result
5045```
5146
5247
5348```
54- Runtime: 556 ms, faster than 37.57 % of Python3 online submissions for Combinations.
55- Memory Usage: 16.2 MB, less than 33.85% of Python3 online submissions for Combinations.
49+ Runtime: 544 ms, faster than 43.06 % of Python3 online submissions for Combinations.
50+ Memory Usage: 15.6 MB, less than 33.85% of Python3 online submissions for Combinations.
5651```
You can’t perform that action at this time.
0 commit comments