class Solution {
public:
string ReverseSentence(string str) {
for (int i = 0, j = str.size() - 1; i < j; i++, j--)
swap(str[i], str[j]);
for (int i = 0; i < str.size(); i++){
int j = i;
while (j < str.size() && str[j] != ' ') j++;
reverse(str.begin() + i, str.begin() + j);
i = j;
}
return str;
}
};
class Solution {
public:
string reverseWords(string s) {
int k = 0;
int n = s.size();
for (int i = 0; i < n; i++){
while (i < n && s[i] == ' ') i++;
if (i == n) break;
int j = i;
while (j < n && s[j] != ' ') j++;
reverse(s.begin() + i, s.begin() + j);
if (k) s[k++] = ' ';
while (i < j) s[k++] = s[i++];
}
s.erase(s.begin() + k, s.end());
reverse(s.begin(), s.end());
return s;
}
};