题目:https://leetcode-cn.com/problems/reverse-words-in-a-string-iii/submissions/
代码:
class Solution {
public:
string reverseWords(string s) {
int size = s.size();
string res;
int i = 0;
while (i < size)
{
while (s[i] == ' ')
{
res += s[i];
i++;
}
int b = i;
while (b < size&& s[b] != ' ')
{
b++;
}
int j = b;
while (b>=1&&s[b - 1] != ' ')
{
res += s[b - 1];
b--;
}
i = j;
}
return res;
}
};