题目链接:https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof/
目录
方法1:调用python中str类的.replace()函数
题目:
方法1:调用python中str类的.replace()函数
通过调用python中str类的.replace()函数,直接完成字符串的替换
class Solution(object):
def replaceSpace(self, s):
"""
:type s: str
:rtype: str
"""
s = s.replace(' ', '%20')
#print(s)
return s
方法2:字符串模拟方法
class Solution(object):
def replaceSpace(self, s):
"""
:type s: str
:rtype: str
"""
returnStr = ''
for i in range(len(s)):
if s[i] != ' ':
returnStr += s[i]
else:
returnStr += '%20'
return returnStr