[C++] STL Solution

  • 0
    • Reverse the string in place.
    • Find the space after the end of current word
    • reverse current word
    • move the beginning of the current word after the space
    class Solution {
        string reverseWords(string s) {
            auto it = s.begin();
            while(it < s.end()){
                auto next = find(it, s.end(), ' ');
                reverse(it, next);
                it = ++next;
            return s;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.