Sword Finger Offer Interview Question 58-I. Flipping the Word Order [Simple] —— istringstream

Same as LeetCode151.

My solution:

Note that s is empty or there are only spaces

class Solution {
public:
    string reverseWords(string s) {
        if(s.empty())   return "";
        istringstream word(s);
        string w;
        string res;
        while(word>>w){
            res=w+' '+res;
        }
        return res.empty()?"":string(res.begin(),res.end()-1);
    }
};

Published 65 original articles · praised 1 · visits 482

Guess you like

Origin blog.csdn.net/qq_41041762/article/details/105478590
Recommended