题解 | #字符串的排列#

字符串的排列

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

全部评论

相关推荐

07-11 13:16
湖南工学院 Java
点赞 评论 收藏
分享
07-10 13:59
门头沟学院 Java
点赞 评论 收藏
分享
06-26 15:33
青岛工学院 Java
积极的秋田犬要冲国企:他现在邀请我明天面试
点赞 评论 收藏
分享
07-07 11:33
江南大学 Java
已经在暑假实习了 ,没有明确说有hc,纠结实习到八月份会不会有点影响秋招毕竟感觉今年好多提前批
程序员小白条:92的话准备提前批,其他没必要,没面试机会的,而且你要准备充分,尤其八股和算法题
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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