[编程题]替换空格
替换空格
http://www.nowcoder.com/questionTerminal/4060ac7e3e404ad1a894ef3e17650423
直接的思路就是将字符串转为列表,然后遍历列表中的空格,如果有就用%20替换,最后再将列表中的各个元素组合起来形成一个新字符串。
特殊情况的判定:空字串。直接返回空字符串即可。
python代码实现如下:
# -*- coding:utf-8 -*- class Solution: # s 源字符串 def replaceSpace(self, s): # write code here if len(s) == 0: return ''; s = list(s) for idx,data in enumerate(s): if data == ' ': s[idx] = '%20' temp = '' for i in s: temp = temp + i return temp