void reverseWords(string &s) {
int word_beg = 0, word_end = s.length()-1;
string new_s;
int i = s.length()-1;
while (i >= 0)
{
while (i >= 0 && s[i] == ' ')
{
i--;
}
word_end = i;
while (i >= 0 && s[i] != ' ')
{
i--;
}
word_beg = i+1;
if (word_beg <= word_end)
{
new_s += s.substr(word_beg, word_end-word_beg+1) + ' ';
}
}
s = new_s.substr(0, new_s.length()-1);
}
[LeetCode] Reverse Words in a String
最新推荐文章于 2014-07-24 20:29:30 发布