C++ in-place concise solution.


  • 0
    C
    void reverseWords(string &s) {
        int l = 0, r = s.size()-1;
        reverse(s, l, r);
        while (l <= r) {
            int pre = l;
            while (l <= r && !isspace(s[l]))
                l++;
            reverse(s, pre, l-1);
            l++;
        }
    }
    
    void reverse(string& s, int l, int r) {
        while (l < r)
            swap(s[l++], s[r--]);
    }

Log in to reply
 

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