![在这里插入图片描述](https://img-blog.csdnimg.cn/20210108184104915.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0ZlTkdRaUh1QUxPVkU=,size_16,color_FFFFFF,t_70)
#include<string>
using namespace std;
class Solution {
public:
string replaceSpace(string s) {
int pos = s.find(" ");
while( pos != -1 )
{
s = s.replace(pos,1,"%20");
pos = s.find(" ");
}
return s;
}
};
class Solution {
public:
string replaceSpace(string s) {
int count = 0;
int sOldSize = s.size();
for (int i = 0; i < s.size(); i++) {
if (s[i] == ' ') {
count++;
}
}
s.resize(s.size() + count * 2);
int sNewSize = s.size();
for (int i = sNewSize - 1, j = sOldSize - 1; j < i; i--, j--) {
if (s[j] != ' ') {
s[i] = s[j];
} else {
s[i] = '0';
s[i - 1] = '2';
s[i - 2] = '%';
i -= 2;
}
}
return s;
}
};
class Solution:
def replaceSpace(self, s: str) -> str:
return s.replace(" ", "%20")