取巧使用字符串的替换方法。
class Solution:
def replaceSpace(self, s: str) -> str:
s=s.replace(' ','%20')
return s
别人的方法,申请一个新的列表,用新增代替替换。重新构建一个字符串。
class Solution:
def replaceSpace(self, s: str) -> str:
res = []
for c in s:
if c == ' ': res.append("%20")
else: res.append(c)
return "".join(res)
作者:jyd
链接:https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof/solution/mian-shi-ti-05-ti-huan-kong-ge-ji-jian-qing-xi-tu-/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。