牛客网题目链接:https://www.nowcoder.com/practice/4060ac7e3e404ad1a894ef3e17650423?tpId=13&tqId=11155&tPage=1&rp=1&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking
题目:请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
代码实现:
class Solution:
# s 源字符串
def replaceSpace(self, s):
newstr = ''
times = 0
iden = []
for i in range(len(s)):
if s[i]==' ':
times += 1
iden.append(i)
for j in range(len(s)):
if j in iden:
newstr = newstr + '%20'
else:
newstr = newstr + s[j]
return newstr
# write code here