0557.cpp (372B)
1 class Solution { 2 public: 3 string reverseWords(string &s) { 4 s.push_back(' '); 5 for (int k = 0, last = -1; k <= s.size(); k++) { 6 if (s[k] != ' ') continue; 7 int i = last + 1, j = k - 1; 8 while (i < j) 9 swap(s[i++], s[j--]); 10 last = k; 11 } 12 s.pop_back(); 13 return s; 14 } 15 };