File tree Expand file tree Collapse file tree 1 file changed +13
-15
lines changed Expand file tree Collapse file tree 1 file changed +13
-15
lines changed Original file line number Diff line number Diff line change @@ -370,23 +370,21 @@ class Solution {
370
370
371
371
372
372
Python:
373
- ``` python
373
+ ``` python3
374
374
class Solution :
375
- result: List[List[int ]] = []
376
- path: List[int ] = []
377
375
def combine (self , n : int , k : int ) -> List[List[int ]]:
378
- self .result = []
379
- self .combineHelper(n, k, 1 )
380
- return self .result
381
-
382
- def combineHelper ( self , n : int , k : int , startIndex : int ):
383
- if (l := len ( self .path)) == k:
384
- self .result.append( self .path.copy())
385
- return
386
- for i in range (startIndex, n - (k - l) + 2 ):
387
- self . path.append(i)
388
- self .combineHelper (n, k, i + 1 )
389
- self .path.pop()
376
+ res = [] # 存放符合条件结果的集合
377
+ path = [] # 用来存放符合条件结果
378
+ def backtrack ( n , k , startIndex ):
379
+ if len (path) == k:
380
+ res.append(path[:])
381
+ return
382
+ for i in range (startIndex,n + 1 ):
383
+ path.append(i) # 处理节点
384
+ backtrack(n,k,i + 1 ) # 递归
385
+ path.pop() # 回溯,撤销处理的节点
386
+ backtrack (n,k, 1 )
387
+ return res
390
388
```
391
389
javascript
392
390
``` javascript
You can’t perform that action at this time.
0 commit comments