题解 | #字符串的排列#

字符串的排列

https://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7?tpId=295&tqId=23291&ru=/exam/oj&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2Foj%3Ftab%3DSQL%25E7%25AF%2587%26topicId%3D82

class Solution:
    def Permutation(self , str):
        if len(str) == 1:
            return list(str)
        distinct_str = ''.join(set(str))
        result_list = [] 
        for i in range(len(distinct_str)):#遍历首字符(需要distinct)
            result_str = str.replace(distinct_str[i], '', 1)
            first = distinct_str[i]            
            for item in self.Permutation(result_str): #一次递归
                result_list.append(first+item) #合并首字符和其他字符组合的list
        return result_list

全部评论

相关推荐

10-30 19:23
已编辑
山东大学(威海) C++
牛至超人:其实简历是不需要事无巨细的写的,让对方知道你有这段经历就行了,最重要的是面试的时候讲细讲明白
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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