题解 | #字符串的排列#
字符串的排列
https://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7
class Solution:
# 回溯法
def Permutation(self , str: str) -> List[str]:
def backtrack(position):
# 到最后,添加
if position == len(s) - 1:
res.append(''.join(s))
return
# 设置一个dic降重
dic = set()
for index in range(position, len(s)):
if s[index] in dic:
continue
dic.add(s[index])
s[index], s[position] = s[position], s[index]
backtrack(position + 1)
# 交换完要还原,以进行其他可能的交换
s[index], s[position] = s[position], s[index]
res = []
s = list(str)
backtrack(0)
return res
联想公司福利 1479人发布
