老卫带你学---剑指offer刷题系列(27.字符串的排列)

27.字符串的排列

问题:

输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。

解决:

思想:

这道题我们直接使用python中的迭代器,里面的permutations函数可以根据输入的字符,进行排列。

python代码:

# -*- coding:utf-8 -*-
from itertools import permutations
class Solution:
    def Permutation(self, ss):
        # write code here
        if not ss:
            return []
        return sorted(list(set(map(''.join,permutations(ss)))))
发布了160 篇原创文章 · 获赞 30 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/yixieling4397/article/details/104961821