1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| class Solution { public: void reverse(string& s, int start, int end){ for (int i = start, j = end; i < j; i++, j--) { swap(s[i], s[j]); } }
void removeExtraSpaces(string& s) { int slow = 0; for (int i = 0; i < s.size(); ++i) { if (s[i] != ' ') { if (slow != 0) s[slow++] = ' '; while (i < s.size() && s[i] != ' ') { s[slow++] = s[i++]; } } } s.resize(slow); }
string reverseWords(string s) { removeExtraSpaces(s); reverse(s, 0, s.size() - 1); int start = 0; for (int i = 0; i <= s.size(); ++i) { if (i == s.size() || s[i] == ' ') { reverse(s, start, i - 1); start = i + 1; } } return s; } };
|