题解 | #翻转单词序列#

翻转单词序列

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

class Solution {
public:
    string ReverseSentence(string str) {
        //用ss把每个单词压入数组
        if(str.length() <= 1) return str;
        vector<string> vec;
        string res;
        stringstream ss(str);
        string temp;
        while(ss>>temp){
            vec.push_back(temp);
        }
        //反着输出
        for(auto it = vec.rbegin(); it != vec.rend(); it++){
            res += *it;
            res += ' ';
        }
        res.back() = '\0';
        return res;
    }
};
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-03 16:22
点赞 评论 收藏
分享
牛客38347925...:9,2学生暑期实习失利开始投小厂,给这群人整自信了
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务