mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
@ -370,23 +370,21 @@ class Solution {
|
|||||||
|
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
```python
|
```python3
|
||||||
class Solution:
|
class Solution:
|
||||||
result: List[List[int]] = []
|
|
||||||
path: List[int] = []
|
|
||||||
def combine(self, n: int, k: int) -> List[List[int]]:
|
def combine(self, n: int, k: int) -> List[List[int]]:
|
||||||
self.result = []
|
res=[] #存放符合条件结果的集合
|
||||||
self.combineHelper(n, k, 1)
|
path=[] #用来存放符合条件结果
|
||||||
return self.result
|
def backtrack(n,k,startIndex):
|
||||||
|
if len(path) == k:
|
||||||
def combineHelper(self, n: int, k: int, startIndex: int):
|
res.append(path[:])
|
||||||
if (l := len(self.path)) == k:
|
return
|
||||||
self.result.append(self.path.copy())
|
for i in range(startIndex,n+1):
|
||||||
return
|
path.append(i) #处理节点
|
||||||
for i in range(startIndex, n - (k - l) + 2):
|
backtrack(n,k,i+1) #递归
|
||||||
self.path.append(i)
|
path.pop() #回溯,撤销处理的节点
|
||||||
self.combineHelper(n, k, i + 1)
|
backtrack(n,k,1)
|
||||||
self.path.pop()
|
return res
|
||||||
```
|
```
|
||||||
javascript
|
javascript
|
||||||
```javascript
|
```javascript
|
||||||
|
Reference in New Issue
Block a user