class Solution {
public:
void replaceSpace(char *str,int length) {
if(str==nullptr&&length<=0)
return;
char* cur = str;
int oldlen = 0;
int num = 0;
while(*cur!='\0')
{
oldlen++;
if(*cur==' ')
num++;
cur++;
}
int newlen = oldlen + num*2;
if(newlen < oldlen)
return;
char* p1 = str + oldlen;
char* p2 = str + newlen;
while(p1!=str&&p1!=p2)
{
if(*p1!=' ')
{
*p2-- = *p1--;
}
else
{
*p2-- = '0';
*p2-- = '2';
*p2-- = '%';
*p1--;
}
}
if(*p1==' ')
{
*p2-- = '0';
*p2-- = '2';
*p2 = '%';
}
}
};
剑指offer面试题 空格替换
最新推荐文章于 2020-12-29 15:53:25 发布