題目描述
無重複字符串的排列組合。編寫一種方法,計算某字符串的所有排列組合,字符串每個字符均不相同。
算法思路
標準回溯算法。
class Solution:
def permutation(self, S: str) -> List[str]:
res=[]
S=list(S)
n=len(S)
def helper(S,n,s=''):
if len(s)==n:
res.append(s)
for i in range(len(S)):
helper(S[:i]+S[i+1:],n,s+S[i])
helper(S,n)
return res
執行用時 :176 ms, 在所有 Python3 提交中擊敗了47.59%的用戶
內存消耗 :20.9 MB, 在所有 Python3 提交中擊敗了100.00%的用戶