zoukankan      html  css  js  c++  java
  • 字符串反转

    一、反转字符串

    思路1:采用中间变量替换

    思路2:采用异或的形式【注意:python中字符串不能直接异或,需要采用ord和chr来转化】

    #反转字符串
    def reverseStr(s):
        if not s:
            return s
        ch = list(s)
        i , j = 0 , len(s)-1
        while i < j:
            ch[i] = chr(ord(ch[i]) ^ ord(ch[j]))
            ch[j] = chr(ord(ch[i]) ^ ord(ch[j]))
            ch[i] = chr(ord(ch[i]) ^ ord(ch[j]))
            # ch[i] = ch[i] ^ ch[j]  【错误的形式】
            # ch[j] = ch[i] ^ ch[j]
            # ch[i] = ch[i] ^ ch[j]
            i += 1
            j -= 1
        return "".join(ch)
    
    s = 'abcdef'
    print(reverseStr(s))

    二、反转单词

    思路:整体全部翻转,再每个单词翻转

    代码:

    #反转字符串
    def reverseStr(s):
        if not s:
            return s
        ch = list(s)
        i , j = 0 , len(s)-1
        while i < j:
            ch[i] = chr(ord(ch[i]) ^ ord(ch[j]))
            ch[j] = chr(ord(ch[i]) ^ ord(ch[j]))
            ch[i] = chr(ord(ch[i]) ^ ord(ch[j]))
            i += 1
            j -= 1
        return "".join(ch)
    
    #s = 'abcdef'
    #print(reverseStr(s))
    
    #将句子反转,单词不反转。
    def reverseWord(s):
        if not s:
            return s
        rs = reverseStr(s)
        arr = rs.split(' ')
        res = []
        for a in arr:
            res.append(reverseStr(a))
        return ' '.join(res)
    
    s = 'I love dogs'
    print(reverseWord(s))

    代码2:

    复制代码
    def rotateWord(words):
        if words == None or len(words) == 0:
            return
        words = list(words[::-1])
        l , r = -1 , -1
        for i in range(len(words)):
            if words[i] != ' ':
                l = i if (i == 0 or words[i-1] ==' ') else l
                r = i if (i == len(words)-1 or words[i+1] == ' ') else r
            if l != -1 and r != -1:
                # reverse(words,l,r)
                words[l:r+1] = words[l:r+1][::-1]
                l , r = -1 , -1
        return ''.join(words)
        
    # def reverse(words,l,r):
    #     tmp = ''
    #     while l<r:
    #         tmp = words[l]
    #         words[l] = words[r]
    #         words[r] = tmp
    #         l += 1
    #         r -= 1
    words = 'dogs love pigs'
    rotateWord(words)
    复制代码

     

    题目:循环右移字符串:

    思路一:三步反转法 :(X^TY^T)^T=YX

    代码:

    ##将字符串的size大小的左半区移到右边,如size=3,“ABCDE”变成“DEABC”
    ###三步反转法
    def reverseStr(s):
        if not s:
            return s
        ch = list(s)
        i , j = 0, len(s)-1
        while i < j:
            ch[i] = chr(ord(ch[i])^ord(ch[j]))
            ch[j] = chr(ord(ch[i])^ord(ch[j]))
            ch[i] = chr(ord(ch[i])^ord(ch[j]))
            i += 1
            j -= 1
        return "".join(ch)
    def reroteStr(s,size):
        if not s:
            return s
        if size > len(s):
            return s
        s = reverseStr(s)
        n = len(s) - size
        left = reverseStr(s[:n])
        right = reverseStr(s[size-1:])
        return left + right
    
    s = 'ABCDE'
    size = 3
    print(reroteStr(s,size))

    题目:旋转10次字符串

    def relote(s):
        for i in range(10):
            s = s[1:] + s[0]
        return s

    题目:把一个字符串的小写字母放到前面,大写放到后面,保持原有的顺序。(python)

    思路:首尾指针查找大小写字母,替换

    代码:

    def sortStr(ss):
        if len(ss)<=1:
            return ss
        s = list(ss)
        i = 0
        j = len(s)-1
        while i != j:
            if s[j].islower():
                s[i] , s[j] = s[j] , s[i]
                i += 1
            elif s[j].isupper():
                j -= 1
        return ''.join(s)
    s = 'aHjDFksn'
    print(sortStr(s))
  • 相关阅读:
    学习进度笔记
    博雅数据机器学习07
    学习进度笔记
    博雅数据机器学习06
    《一级架构师》阅读笔记
    学习进度笔记
    博雅数据机器学习05
    递归的概念
    CSS学习笔记3:选择器及优先级
    CSS学习笔记2:伪类
  • 原文地址:https://www.cnblogs.com/Lee-yl/p/10463243.html
Copyright © 2011-2022 走看看