题目描述
URL化。编写一种方法,将字符串中的空格全部替换为%20。假定该字符串尾部有足够的空间存放新增字符,并且知道字符串的“真实”长度。(注:用Java实现的话,请使用字符数组实现,以便直接在数组上操作。)
范例:
输入:"Mr John Smith ", 13
输出:"Mr%20John%20Smith"
输入:" ", 5
输出:"%20%20%20%20%20"
思路分析:
我们只需要在字符长度范围内的空格替换为“%20"即可
class Solution(object):
def replaceSpaces(self, S, length):
"""
:type S: str
:type length: int
:rtype: str
"""
return S[:length].replace(' ','%20')