C++ solution using istringstream


  • 0
    B
    class Solution { 
    public:
        void reverseWords(string &s) {
            istringstream in(s);
            string tmp;
            stack<string> st;
            while(in >> tmp){
                st.push(tmp);
            }
            tmp = "";
            while(!st.empty()){
                tmp = tmp + st.top() + " ";
                st.pop();
            }
            int n = s.size();
            s = tmp.substr(0, n);
        } 
    };
    

Log in to reply
 

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