时间复杂度降为O(n)
#include <iostream>
#include <string>
using namespace std;
char* delSpace(char * str)
{
char *p = str; // p和t在指向同一地方
char *t = p;
char *temp = p;//保存首地址
while(*p!='\0')
{
if(*p==' ')
{
t++; // t遍历
if(*t!=' ')//找到第一不为空格的数
{
*p = *t; //跟空格进行交换
*t = ' ';
}
}
else
{
p++;
t = p;
}
}
return temp;
}
int main()
{
char str[ ] = " wod css rld fgdfgdf dfdss ";
char *p = delSpace(str);
cout<<p<<endl;
getchar();
}