跳转至

面试题 01.03. URL化#

问题描述#

URL化。编写一种方法,将字符串中的空格全部替换为%20。假定该字符串尾部有足够的空间存放新增字符,并且知道字符串的“真实”长度。(注:用Java实现的话,请使用字符数组实现,以便直接在数组上操作。)

 

示例 1:


输入:"Mr John Smith    ", 13
输出:"Mr%20John%20Smith"

示例 2:


输入:"               ", 5
输出:"%20%20%20%20%20"

 

提示:

  • 字符串长度在 [0, 500000] 范围内。

解题思路#

直接使用 str 自带的 replace 方法。

1
2
3
class Solution:
    def replaceSpaces(self, S: str, length: int) -> str:
        return S[:length].replace(" ", "%20")

从后往前遍历。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution:
    def replaceSpaces(self, S: str, length: int) -> str:
        S = list(S)
        n = len(S)
        for c in S[length-1::-1]:
            if c != " ":
                n -= 1
                S[n] = c
            else:
                n -= 3
                S[n:n+3] = "%20"
        return "".join(S[n:])
返回顶部

在手机上阅读