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