题解 | #字符串的排列#
字符串的排列
http://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7
由于字符串的内置函数较少,将字符串转化为列表,方便对其进行remove、append、insert等操作,输出结果时使用join方法将列表转化为字符串
class Solution:
def Permutation(self , str: str) -> List[str]:
# write code here
result = []
temp = []
#num.sort()
len_ = len(str)
if not len_:
return result
if len_ == 1:
result.append(str)
return result
str_list = list(str)
def check(array):
if len(temp) == len_:
temp2 = ''.join(temp)
if temp2 not in result:
result.append(temp2)
else:
for i in range(len(array)):
a = array[i]
temp.append(a)
array.remove(a)
check(array)
array.insert(i,a)
temp.pop()
check(str_list)
return result