首页 > 其他 > 详细

字符串反转

时间:2019-03-02 22:35:33      阅读:255      评论:0      收藏:0      [点我收藏+]

 

一、反转字符串

思路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))

 

题目:把一个字符串的小写字母放到前面,大写放到后面,保持原有的顺序。(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))

 

字符串反转

原文:https://www.cnblogs.com/Lee-yl/p/10463243.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!