AcWing 16. 替换空格
原题链接
简单
作者:
哈士奇真可爱
,
2021-05-08 16:43:24
,
所有人可见
,
阅读 611
class Solution {
public:
string replaceSpaces(string &str) {
string res;
for(auto x:str)
if(x == ' ')
res+="%20";
else
res+=x;
return res;
}
};
class Solution {
public:
string replaceSpaces(string &str) {
int len = 0;
for(auto c:str)
{
if(c == ' ')
len+=3;
else
len++;
}
int i =str.size() - 1,j = len-1;
str.resize(len);//必须重置长度,要不然数组溢出
while(i>=0)
{
if(str[i]==' ')
{
str[j--]='0';
str[j--]='2';
str[j--]='%';
}
else
str[j--]=str[i];
i--;
}
return str;
}
};
牛批
强!!!
orz