剑指 替换空格
替换空格
http://www.nowcoder.com/questionTerminal/0e26e5551f2b489b9f58bc83aa4b6c68
s.split
class Solution: def replaceSpace(self , s ): # write code here result='' for item in s.split(' '): result+=item result+='%20' return result[:-3]
s.replace
循环判断有空格
双指针
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param s string字符串 # @return string字符串 # class Solution: def replaceSpace(self , s ): # write code here if len(s)==0: return s else: s=list(s) count=0 for item in s: if item==' ': count+=1 left=len(s)-1 for i in range(count*2): s.append(' ') right=len(s)-1 while left>=0: if s[left]==" ": s[right-2:right+1]='%20' right=right-3 left-=1 else: s[right]=s[left] left-=1 right-=1 str_s='' for item in s: str_s+=item return str_s