题解 | #字符串的排列#

字符串的排列

https://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param str string字符串
# @return string字符串一维数组
#
class Solution:
    def Permutation(self, str: str) -> list[str]:
        # write code here
        path = []

        n = len(str)

        L2 = list(str)
        ans = []
        L4 = []
        visited = [False for i in range(n)]
	#要记得排序。排序了才可以用到那个剪短枝条的公式
        L2.sort()

        def MyPermutation(L2, L, ans):
            if len(L) == n:
                ans.append(path.copy())
                return

            else:
                for i in range(len(L2)):
                    if visited[i]:
                        continue
                    if i>0 and L2[i-1]==L2[i] and visited[i-1]==False:
                        continue

                    path.append(L2[i])
                    visited[i] = True
                    MyPermutation(L2, L, ans)
                    path.pop()
                    visited[i] = False

        MyPermutation(L2, path, ans)
        for x in ans:
            L4.append("".join(x))
        return L4

全部评论

相关推荐

流浪的神仙:无恶意,算法一般好像都得9硕才能干算法太卷啦
点赞 评论 收藏
分享
06-12 17:46
门头沟学院 Java
运营你豪哥:来说重点: ​1.项目前置,时间倒序。​​ 2.​项目描述强化结果与量化效果(STAR原则里的R)。​​ ​3.个人技能精炼,明确掌握程度,突出核心。​​ ​4.增加强有力开头的个人总结部分。​​ 5.​优化教育背景(成绩排名)、合并奖项与活动。​​
听劝,我这个简历该怎么改...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务