反转字符串
输入: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
纸上得来终觉浅,绝知此事要躬行。
17688689121
我的微信
微信扫一扫
评论