美文网首页
字符串倒序

字符串倒序

作者: barriers | 来源:发表于2019-04-09 16:30 被阅读0次

    1.将一个字符串倒序

    def str_reverse(s):
        return s[::-1]
    
    def func2(s):
        list1 = list(s)
        list1.reverse()
        return ''.join(list1)
    
    def func3(s):
        list1 = []
        for i in range(len(s)-1,-1,-1):
            list1.append(s[i])
        return ''.join(list1)
    
    def func4(s):
        if len(s)<=1:
            return s
        return s[-1] + func4(s[:-1])
    
    def rever(st):
        b = len(st) >> 1
        d = list(st)
        c = len(st)
        for i in range(b):
            d[i], d[c - 1 - i] = d[c - 1 - i], d[i]
        return ''.join(d)
    
    a = '123321'
    b = '12321'
    print(rever(a)) # 123321
    print(rever(b)) # 12321

    相关文章

      网友评论

          本文标题:字符串倒序

          本文链接:https://www.haomeiwen.com/subject/srkiiqtx.html