题解 | #翻转单词序列#(C++stringstream输入流)

翻转单词序列

http://www.nowcoder.com/practice/3194a4f4cf814f63919d0790578d51f3

class Solution {
public:
    string ReverseSentence(string str) {
        stack<string> s;
        stringstream in(str);
        string a;
        while(in >> a)
            s.push(a);
        a = "";
        while(!s.empty()) {
            a += s.top();
            if(s.size() != 1) a += " ";
            s.pop();
        }
        return a;
    }
};
全部评论

相关推荐

点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务