(1)剑指 Offer 05. 替换空格
解题思路:将要扩充的空间先补上,然后使用双指针非空格往后扔,空格留三个补充%20
class Solution:
def replaceSpace(self, s: str) -> str:
counter = s.count(' ')
res = list(s)
res.extend([' '] * counter * 2)
left, right = len(s)-1, len(res)-1
while left >= 0:
if res[left] != ' ':
res[right] = res[left]
right -= 1
else:
# print(right-2, right+1)
res[right-2:right+1] = '%20'
right -= 3
left -= 1
return "".join(res)