Posted on

Description

Submission

class Solution {
public:
    string reverseWords(string s) {

        string cur = "", ret = "";
        for(auto ch: s) {
            if(ch == ' ') {
                if(!ret.empty()) ret += " ";
                reverse_copy(cur.begin(), cur.end(), back_inserter(ret));
                cur = "";
            } else {
                cur.push_back(ch);
            }
        }

        if(!cur.empty()) {
            if(!ret.empty()) ret += " ";
            reverse_copy(cur.begin(), cur.end(), back_inserter(ret));
        }

        return ret;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *