close
題目
Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
Example 1:
Input: "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc"
想法
看到空白的時候,就將前面的字串做反轉
code
char* reverseWords(char* s) {
int i, j, k;
int head = 0;
int tail;
char tmp;
for(i=0; ; i++)
{
if(s[i]==' ' || s[i] == '\0'){
tail=i-1;
for(j=head, k=tail; k>j; j++, k--) {
tmp = s[j];
s[j] = s[k];
s[k] = tmp;
}
if(s[i]==' ')
head = i+1;
else
break;
}
}
return s;
}
文章標籤
全站熱搜