Skip to content

Commit be9be7d

Browse files
authored
Update 77_Combinations.md
1 parent 3bbd4f5 commit be9be7d

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

77_Combinations.md

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,21 @@ Constraints:
3131

3232
```python
3333
def 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
```

0 commit comments

Comments
 (0)