字符串反转

 

Top~~

一、反转字符串

思路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))
复制代码

 

Top~~

二、反转单词

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

Top~~

代码:

复制代码
#反转字符串
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)
复制代码

 

复制代码
Top~~

题目:循环右移字符串:

思路一:三步反转法 :(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))
复制代码
Top~~

题目:旋转10次字符串

 

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

 

 

 

Top~~

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

 

posted on   吱吱了了  阅读(956)  评论(0编辑  收藏  举报

编辑推荐:
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
阅读排行:
· Sdcb Chats 技术博客:数据库 ID 选型的曲折之路 - 从 Guid 到自增 ID,再到
· 语音处理 开源项目 EchoSharp
· 《HelloGitHub》第 106 期
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 使用 Dify + LLM 构建精确任务处理应用
< 2025年1月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8

导航

统计

点击右上角即可分享
微信分享提示