STL style C++ Solution

  • 0

    Here is a simple O(n) solution using std::find and std::reverse in a do-while loop.

    string reverseWords(string s) {
        auto iter = s.begin();
        auto last = s.begin();
            last = std::find(iter, s.end(), ' ');
            std::reverse(iter, last);
        }while((iter = last)++ != s.end());
        return s;

Log in to reply

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