1 题目描述
请实现一个函数,把字符串 s
中的每个空格替换成"%20"。
2 思路
c++:遍历一遍
python:replace
3 代码
c++:
class Solution {
public:
string replaceSpace(string s) {
string str;
for (int i=0; i < s.length(); i++) {
if (s[i] == ' ') {
str += "%20";
}
else{
str += s[i];
}
}
return str;
}
};
python:
class Solution(object):
def replaceSpace(self, s):
return s.replace(' ', '%20')