提交 e95794b2 编写于 作者: Z zongshuai818 提交者: labuladong

增加回溯算法之全排列python3解法

上级 0c57a909
......@@ -278,6 +278,28 @@ def backtrack(...):
![labuladong](../pictures/labuladong.png)
[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)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册