C++简单代码/10行:

翻转单词顺序列

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

C++简单代码/10行:

class Solution {
public:
    string ReverseSentence(string s) {
        for(int i = 0; i < s.size() && s[i] == ' ';i ++)
            if (i == s.size() - 1 ) return s;
        stack<string> stk;
        string res,str;
        istringstream ss(s);
        if (s == " ") return res + " ";
        while (ss >> str) stk.push(str), stk.push(" ");
        if (!stk.empty()) stk.pop();
        while (!stk.empty()) res += stk.top(), stk.pop();
        return  res;
    }
};
全部评论

相关推荐

小覃1:硕士了还投助理岗位吗,一般不都直接干工程师了吗
点赞 评论 收藏
分享
03-28 22:31
门头沟学院 Java
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务