1 Star 0 Fork 1.5K

沐瑶 / LearningNotes

forked from 陌溪 / LearningNotes 
Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
Clone or download
README.md 917 Bytes
Copy Edit Web IDE Raw Blame History
陌溪 authored 2020-04-23 10:53 . 增加剑指offer相关算法学习

替换空格

题目描述

请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。

调用方法实现

class Solution:
    # s 源字符串
    def replaceSpace(self, s):
        return s.replace(' ', '%20')

自己写方法实现Replace

我们将字符串存储在数组中,然后循环比较每个字符的值,当遇到空格的时候,替换即可

class Solution:
    def replaceSpace2(self, s):
        strLen = len(s)
        aaa = []
        for i in range(0, strLen):
            if s[i] == " ":
                aaa.append("%")
                aaa.append("2")
                aaa.append("0")
            else:
                aaa.append(s[i])
        return ''.join(aaa)

if __name__ == '__main__':
    print(Solution().replaceSpace2('We Are Happy'))

Comment ( 0 )

Sign in for post a comment

1
https://gitee.com/muyao_vip/LearningNotes.git
git@gitee.com:muyao_vip/LearningNotes.git
muyao_vip
LearningNotes
LearningNotes
master

Search

102255 3a0e046c 1850385 102255 7aaa926c 1850385