代码随想录05:字符串

BBigSun 评论178阅读模式

代码随想录05:字符串

反转字符串

输入:hello文章源自十年又十年-https://www.bbigsun.com/411.html

输出:olleh文章源自十年又十年-https://www.bbigsun.com/411.html

思路:双指针法,头尾双指针,互相交换,然后向中间平移,当头尾指针相交时结束文章源自十年又十年-https://www.bbigsun.com/411.html

def reverse(s):
    first = 0
    end = len(s)-1
    while first < end:
        s[first], s[end] = s[end],s[first]
        first += 1
        end -= 1

另外一种:文章源自十年又十年-https://www.bbigsun.com/411.html

s = s[::-1]

替换空格

输入:"Hello World"
输出:"Hello%20World"文章源自十年又十年-https://www.bbigsun.com/411.html

做题的做法:文章源自十年又十年-https://www.bbigsun.com/411.html

s = "20%".join(s.split(" "))

善用切片文章源自十年又十年-https://www.bbigsun.com/411.html 文章源自十年又十年-https://www.bbigsun.com/411.html

纸上得来终觉浅,绝知此事要躬行。

weinxin
17688689121
我的微信
微信扫一扫
BBigSun
  • 本文由 BBigSun 发表于 2023年 3月 31日 08:12:39
  • 转载请务必保留本文链接:https://www.bbigsun.com/411.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定