Leetcode剑指 Offer 05. 替换空格
文章目录
- 题目要求

思路:
- 遍历字符串,遇到空格替换
- 因为字符串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
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论