- 题目要求
思路:
- 遍历字符串,遇到空格替换
- 因为字符串s长度可变,所以使用的是while循环,实时更新字符串的长度
- 完整代码:
class Solution:
def replaceSpace(self, s: str) -> str:
cur, lenth = 0, len(s)
while cur < lenth:
if s[cur] == " ":
s = s[:cur] + "%20" + s[cur + 1:]
cur += 1
lenth = len(s)
return s
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。