Leetcode 186 Solution

This article provides solution to leetcode question 186 (reverse-words-in-a-string-ii)

https://leetcode.com/problems/reverse-words-in-a-string-ii

Solution

class Solution { public: void reverseWords(string &s) { int i = 0;
while (true) { auto end = s.find_first_of(' ', i);
if (end == string::npos) { reverse(&s[i], &s[s.size()]); break; } else { reverse(&s[i], &s[end]); i = end + 1; } }
reverse(s.begin(), s.end()); } };