class Solution
{
public:
string replaceSpace(string s)
{
int count = 0;
for (int i = 0; i < s.size(); ++i)//统计空格的数量
{
if (s[i] == ' ')
++count;
}
int sOldNum = s.size();
s.resize(sOldNum + 2 * count);//改变字符串的大小
int sNewNum = s.size();
for (int i = sNewNum - 1, j = sOldNum - 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;
}
};