题解 | #翻转单词序列#

翻转单词序列

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

class Solution {
public:
    string ReverseSentence(string str) {
        string result;
        stack<char> st1;
        stack<char> st2;
        for (auto s : str)
            st1.push(s);
        while (!st1.empty()){
            while (!st1.empty() && st1.top() != ' '){
                st2.push(st1.top());
                st1.pop();
            }
            while (!st2.empty()){
                result += st2.top();
                st2.pop();
            }
            if (!st1.empty()){
                if (st1.top() == ' '){
                    result += st1.top();
                    st1.pop();
                }
            }
        }
        return result;
    }
};

全部评论

相关推荐

Rena1ssanc...:对的,要是面评没太烂,勤更新简历等捞就行了,腾讯可以无限复活
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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