Simple and concise C++ solution


  • 1
    A
    class Solution {
    public:
        void reverseWords(string &s) {
            reverse(s.begin(), s.end());
            
            for (string::iterator b = s.begin(), e = s.begin(); e <= s.end(); ++e) {
                if (e == s.end() || *e == ' ') {
                    reverse(b, e);
                    b = e + 1;
                }
            }
        }
    };

Log in to reply
 

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