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