Leetcode Link: 剑指 Offer 05. 替换空格 - 力扣(LeetCode)

题目

请实现一个函数,把字符串 s 中的每个空格替换成”%20”。

解法一

思路:自带函数模拟

题解

class Solution:
    def replaceSpace(self, s: str) -> str:
        return s.replace(" ""%20")

解法二

思路 不用自带函数,遍历就行

题解:

class Solution:
    def replaceSpace(self, s: str) -> str:
        new = ''
        for c in s:
            if c == ' ':
                new = new + '%20'
            else:
                new = new + c
        return new

启发和联系