/* s[head~tail] reverse */
void reverseSubstr(char*s , int head, int tail)
{
char temp = 0;
while(head < tail)
{
temp = s[tail] ;
s[tail] = s[head] ;
s[head] = temp ;
head++ ;
tail-- ;
}
}
char* reverseWords(char * s)
{
int head = 0;
int tail = -2;
int i = 0;
while((s[i]!='\0'))
{
if(s[i]==' ')
{
head = tail + 2;
tail = i-1;
reverseSubstr(s,head,tail);
}
i++;
}
if( ( i >1 )
&&( s[i-1]!=' ' )
)
{
head = tail+2;
tail = i-1;
reverseSubstr(s,head,tail);
}
return s;
}
/*
执行用时 :12 ms, 在所有 C 提交中击败了92.24%的用户
内存消耗 :8.3 MB, 在所有 C 提交中击败了45.10%的用户
*/
|