LeetCode 557.反转字符串中的单词 III
超过99%
void reverse(int from, int to, string &s)
{
char temp;
to--;
for (; from < to; from++)
{
temp = s[from];
s[from] = s[to];
s[to] = temp;
to--;
}
}
string reverseWords(string s) {
int posf = 0, post = 0;
for (int i = 0; i < s.length(); i++)
{
if (s[i] == ' ')
{
reverse(posf, post, s);
posf = i + 1;
post = i + 1;
}
else
post++;
}
if (posf != post)
reverse(posf, post, s);
return s;
}