剑指offer 面试题58 - I. 翻转单词顺序 [简单]——istringstream

与LeetCode151题相同。

我的解题:

注意s为空或者只有空格的情况

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);
    }
};

发布了65 篇原创文章 · 获赞 1 · 访问量 482

猜你喜欢

转载自blog.csdn.net/qq_41041762/article/details/105478590