offer38字符串的排列

offer字符串的排列

题目:输入一个字符串,打印出该字符串中字符的所有排列。 你可以以任意顺序返回这个字符串数组,但里面不能有重复元素。

示例:
输入:s = "abc"
输出:["abc","acb","bac","bca","cab","cba"]

递归

作者:cy助我
链接:https://www.nowcoder.com/questionTerminal/fe6b651b66ae47d7acce78ffdd9a96c7
来源:牛客网

class Solution:
    def Permutation(self, ss):
        # write code here
        if len(ss) <= 1:
            return ss
        res = set()
        for i in range(len(ss)):
            for j in self.Permutation(ss[0:i] + ss[i+1:]):
                res.add(ss[i] + j)
        return sorted(res)

递归过程

作者:海大_Man
链接:https://www.nowcoder.com/questionTerminal/fe6b651b66ae47d7acce78ffdd9a96c7
来源:牛客网

'''
Perm(ss = ["a","b","c"])
  i = 0
      j in perm(ss[0:0] + ss[1:] = ["b","c"])=  perm(ss = ["b","c"])
           perm(ss = ["b","c"])
                  i = 0 
                     j in perm(s[0:0] + s[1:]) = perm("c") = return "c"
                        j ="c", res.add(s[0] + "c" = "b" + "c")  res = ("bc")
                  i = 1
                     j in perm(s[0:1] + s[2:]) = perm("b") = return "b"
                     j = "b" res.append(s[1] + "b" = "c"+"b") res = ("bc",cb")
                  return res = ("bc","cb")
      j in ("bc","cb")
      j = "bc" res.append("a" + "bc"), res= ("abc")
      j = "cb" res.append("a"+"cb"),  res = ("abc",acb")
      
  i = 1 
    j in perm(s[0:1] + s[2:]) = perm(ss = ["a","c"])
       perm(ss = ["a","c"])
        i = 0 
            j in perm(s[0:0] + s[1:]) = perm("c") = return "c"
                j ="c", res.add(s[0] + "c" = "a" + "c")  res = ("ac")
         i = 1
             j in perm(s[0:1] + s[2:]) = perm("a") = return "a"
                  j = "b" res.append(s[1] + "a" = "c"+"a") res = ("ac",ca")
         return res = ("ac","ca")
      j in ("ac","ca")
      j = "ac" res.append("b" + "ac"), res= ("abc",acb","bac")
      j = "ca" res.append("b"+"ca"),  res = ("abc",acb","bac","bca")
      
  i = 2 
    j in perm(s[0:2] + s[3:]) = perm(ss = ["a","b"])
       perm(ss = ["a","b"])
        i = 0 
            j in perm(s[0:0] + s[1:]) = perm("b") = return "b"
                j ="c", res.add(s[0] + "b" = "a" + "b")  res = ("ab")
         i = 1
             j in perm(s[0:1] + s[2:]) = perm("a") = return "a"
                  j = "b" res.append(s[1] + "a" = "b"+"a") res = ("ab",ba")
         return res = ("ab","ba")
      j in ("ab","ba")
      j = "ab" res.append("c" + "ab"), res= ("abc",acb","bac","cab")
      j = "ba" res.append("c"+"ba"),  res = ("abc",acb","bac","bca","cab","cba")
'''
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值