mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
添加 0077.组合优化 python版本
添加 0077.组合优化 python版本
This commit is contained in:
@ -176,8 +176,22 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
```python3
|
||||||
|
class Solution:
|
||||||
|
def combine(self, n: int, k: int) -> List[List[int]]:
|
||||||
|
res=[] #存放符合条件结果的集合
|
||||||
|
path=[] #用来存放符合条件结果
|
||||||
|
def backtrack(n,k,startIndex):
|
||||||
|
if len(path) == k:
|
||||||
|
res.append(path[:])
|
||||||
|
return
|
||||||
|
for i in range(startIndex,n-(k-len(path))+2): #优化的地方
|
||||||
|
path.append(i) #处理节点
|
||||||
|
backtrack(n,k,i+1) #递归
|
||||||
|
path.pop() #回溯,撤销处理的节点
|
||||||
|
backtrack(n,k,1)
|
||||||
|
return res
|
||||||
|
```
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user