mirror of
https://github.com/labuladong/fucking-algorithm.git
synced 2025-07-07 05:44:24 +08:00
增加回溯算法之全排列python3解法
This commit is contained in:
@ -278,6 +278,28 @@ def backtrack(...):
|
||||
|
||||

|
||||
|
||||
[Zongshuai](https://github.com/zongshuai818) 提供全排列 Python3解法代码:
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def permute(self, nums: List[int]) -> List[List[int]]:
|
||||
# 回溯算法
|
||||
result = []
|
||||
track = [] # 可行路径
|
||||
def trackBack(nums_, track_):
|
||||
if len(track_) == len(nums_): # 满足终止条件
|
||||
result.append(track_[:])
|
||||
return
|
||||
for i in nums_: #所有可选项
|
||||
if i in track_: # 判断是否可选
|
||||
continue
|
||||
track.append(i) # 选择
|
||||
trackBack(nums_, track_) # 递归
|
||||
track.pop() # 回溯
|
||||
trackBack(nums, track)
|
||||
return result
|
||||
```
|
||||
|
||||
[上一篇:动态规划答疑篇](../动态规划系列/最优子结构.md)
|
||||
|
||||
[下一篇:二分查找解题框架](../算法思维系列/二分查找详解.md)
|
||||
|
Reference in New Issue
Block a user