- 调用函数
class Solution: # s 源字符串 def replaceSpace(self, s): # write code here if not s: return '' return '%20'.join(s.split(' ')) # return s.replace(' ', '%20')
2.书上的做法
# -*- coding:utf-8 -*- class Solution: # s 源字符串 def replaceSpace(self, s): # write code here k = len(s) n = 0 for i in s: if i == ' ': n += 1 kk = k + 2*n ss = [0]*kk i = k - 1 while i >= 0: if s[i] == ' ': ss[i+2*n] = '0' ss[i-1+2*n] = '2' ss[i-2+2*n] = '%' i -= 1 n -= 1 else: ss[i+2*n] = s[i] i -= 1 return ''.join(ss)