面试题 08.07. 无重复字符串的排列组合



套dfs回溯模板,模板参考:https://www.cnblogs.com/panweiwei/p/14025143.html

class Solution(object):
    def __init__(self):
        # res作为返回值,设为全局变量
        self.res = []

    def permutation(self, S):
        """
        :type S: str
        :rtype: List[str]
        """
        # 获取原串的长度
        n = len(S)
        # 暂存此趟循环的组合串
        temp = []
        # 初始化访问标记数组
        visit = [0 for _ in range(n)]
        # 调用函数
        self.dfs(S, temp, visit, n)
        return self.res

    def dfs(self, Str, temp, visit, n):
        # 定义递归出口:temp长度与原串相等,则一个新的答案诞生
        if len(Str) == len(temp):
            self.res.append("".join(temp))
        # 否则,遍历原串,逐个取得字符拼凑temp
        else:
            for i in range(n):
                # 当且仅当当前字符未被访问过
                if not visit[i]:
                    # 取得字符拼凑temp
                    temp.append(Str[i])
                    # 修改访问标记数组
                    visit[i] = 1
                    # 递归
                    self.dfs(Str, temp, visit, n)
                    # 回退
                    temp.pop(-1)
                    # 标记数组也要回退
                    visit[i] = 0
原文地址:https://www.cnblogs.com/panweiwei/p/14025504.html