请实现一个函数,将一个字符串s中的每个空格替换成“%20”。
例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
数据范围:0<=lens(10)<100。保证字符串中的字符为大写英文字母、小写英文字母和空格中的一种。
进阶:时间复杂度 O(n) ,空间复杂度O(n)
class Solution:
def __init__(self,s):
self.s=s
def replaceSpace(self):
num = []
for i in self.s : #遍历字符串
if i == " ":
num.append("%20")
else:
num.append(i)
return ''.join(num) #这里注意使用join进行格式转换、
str = Solution("We Are Happy")
print(str.replaceSpace())
输出:
We%20Are%20Happy
题目来源:牛客网剑指offer