Leetcode 186 Solution
This article provides solution to leetcode question 186 (reverse-words-in-a-string-ii)
Access this page by simply typing in "lcs 186" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
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());
}
};