2020-06-22
1.题目描述
请实现一个函数,把字符串 s 中的每个空格替换成"%20"。
2.题解
1.res+="%20";
2.res.push_back('%');
res.push_back('2');
res.push_back('0');
class Solution {
public:
string replaceSpace(string s) {
string res="";
int l=s.length();
for (int i=0;i<l;i++){
if (s[i]==' ') res+="%20";
else res+=s[i];
}
return res;
}
};
class Solution {
public:
string replaceSpace(string s) {
int len=s.size();
string res;
int i=0;
while (i<len){
if (s[i]==' '){
res.push_back('%');
res.push_back('2');
res.push_back('0');
}else{
res.push_back(s[i]);
}
i++;
}
return res;
}
};