c++ use streamstring


  • 0
    1
    class Solution {
    public:
        string reverseWords(string s) {
            stringstream ss(s);
            vector<string> vs;
            string tempt = "";
            if(!(ss>>tempt))
            {
                return "";
            }
            do
            {
                reverse(tempt.begin(), tempt.end());
                vs.push_back(tempt);
                vs.push_back(" ");
            }while(ss>>tempt);
            tempt="";
            for(int i = 0; i<vs.size()-1; i++)
                tempt += vs[i];
            return tempt;
        }
    };
    

Log in to reply
 

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