题解 | #JZ73 翻转单词序列#

翻转单词序列

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

class Solution {
public:
    string ReverseSentence(string str) {
        vector<string> strvec;
        string ret;
        
       for (size_t pos=0; str.size() && pos!=string::npos; ) {
           size_t p = str.find_first_of(' ', pos);    //按空格分隔放到向量中
           
           if (p != string::npos) {
               strvec.push_back(str.substr(pos, p-pos));
               pos = p + 1;
           } else {
               strvec.push_back(str.substr(pos));
               pos = p;
           }
       }
       
        for (vector<string>::reverse_iterator it=strvec.rbegin(); it!=strvec.rend(); ++it) {    //反向遍历并组成字符串
            if (ret.size()) ret += " ";
            ret += *it;
        }
        
       return ret;
    }
};
全部评论

相关推荐

2025-12-15 12:50
河北工程大学
sta666:我也是这个国际商业化的,三天,一天一面,就通过了,不过我是后端实习生,好好面感觉能过。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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