1 Star 0 Fork 3.1K

weianlai / LearningNotes

forked from 陌溪 / LearningNotes 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 917 Bytes
一键复制 编辑 原始数据 按行查看 历史
陌溪 提交于 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'))
1
https://gitee.com/walgit/LearningNotes.git
git@gitee.com:walgit/LearningNotes.git
walgit
LearningNotes
LearningNotes
master

搜索帮助