Skip to content

Commit e95794b

Browse files
zongshuai818labuladong
authored andcommitted
增加回溯算法之全排列python3解法
1 parent 0c57a90 commit e95794b

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

算法思维系列/回溯算法详解修订版.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,28 @@ def backtrack(...):
278278

279279
![labuladong](../pictures/labuladong.png)
280280

281+
[Zongshuai](https://github.com/zongshuai818) 提供全排列 Python3解法代码:
282+
283+
```python
284+
class Solution:
285+
def permute(self, nums: List[int]) -> List[List[int]]:
286+
# 回溯算法
287+
result = []
288+
track = [] # 可行路径
289+
def trackBack(nums_, track_):
290+
if len(track_) == len(nums_): # 满足终止条件
291+
result.append(track_[:])
292+
return
293+
for i in nums_: #所有可选项
294+
if i in track_: # 判断是否可选
295+
continue
296+
track.append(i) # 选择
297+
trackBack(nums_, track_) # 递归
298+
track.pop() # 回溯
299+
trackBack(nums, track)
300+
return result
301+
```
302+
281303
[上一篇:动态规划答疑篇](../动态规划系列/最优子结构.md)
282304

283305
[下一篇:二分查找解题框架](../算法思维系列/二分查找详解.md)

0 commit comments

Comments
 (0)