题解 | #字符串的排列#
字符串的排列
http://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7
思路:
遍历当前字符,再遍历除当前字符其他字符的全排列,相加为结果
1、注意重复去除
2、除以递归出口为字符长度为1时,直接返回该字符
class Solution:
def Permutation(self , str: str) -> List[str]:
# write code here
if len(str) == 0:
return []
return self.helper(str)
def helper(self, ss):
if len(ss) <= 1: # 重点是递归出口
return ss
result = []
for i in range(len(ss)):
s1 = ss[i]
for s2 in self.helper(ss[:i]+ss[i+1:]):
s = s1 + s2
if s not in result:
result.append(s)
return result