stack oj题

分享几道栈的oj题,利于进一步掌握stack的使用。

第一题:最小栈

第二题:栈的弹出压入序列

第三题:用栈实现队列

第四题:二叉树的层序遍历

第一题题解

class MinStack {
public:
    MinStack() 
    {

    }

    void push(int val) 
    {
        ins.push(val);
        if(mins.empty()||val<= mins.top())
        {
            mins.push(val);
        }
    }
    
    void pop() 
    {
        if(!mins.empty()&&ins.top() == mins.top())
        {
            mins.pop();
        }
        ins.pop();
    }
    
    int top() 
    {
        return ins.top();
    }
    
    int getMin() 
    {
        assert(!mins.empty());
        return mins.top();
    }
    private:
        stack<int> ins;
        stack<int> mins;

};

第二题题解

class Solution {
public:

    bool IsPopOrder(vector<int>& pushV, vector<int>& popV) 
    {

        size_t pos2=0;

        stack<int> ins;

        for(const auto& e:pushV)
        {
            ins.push(e);
            while(!ins.empty()  &&  ins.top() == popV[pos2])
            {
                ins.pop();
                ++pos2;
            }
        }
        return ins.empty() ;
        
    }
};

第三题题解

class MyQueue {
public:
    MyQueue() {

    }
    
    void push(int x) 
    {  
            while(!outs.empty())
            {
                int tmp = outs.top();
                outs.pop();
                ins.push(tmp);
            }
        ins.push(x);
    }
    
    int pop() {

        while(!ins.empty())
        {
            int tmp =ins.top();
            ins.pop();
            outs.push(tmp);
        }
        int ret = outs.top();
        outs.pop();
        return ret;
    }
    
    int peek() 
    {
        while(!ins.empty())
        {
            int tmp =ins.top();
            ins.pop();
            outs.push(tmp);
        }
        return outs.top();
    }
    
    bool empty() 
    {
        return ins.empty()&&outs.empty();
    }
private:
    stack<int> ins;
    stack<int> outs;
};

第四题题解

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) 
    {
        vector<vector<int>> vv;
        if(root == nullptr)
        {
            return vv;
        }
        int levelsize =1;//每层数据个数
        q1.push(root);

        while(!q1.empty())
        {
            vector<int> v;
             while(levelsize--)//可以控制一层一层出数据
            {
                TreeNode* tmp =q1.front();
                q1.pop();
                v.push_back(tmp->val);
                if(tmp->left)
                {
                    q1.push(tmp->left);
                }
                if(tmp->right)
                {
                    q1.push(tmp->right);
                }
            }
            levelsize =q1.size();
            //当前层出完了,将v插入vv
            vv.push_back(v);

        }
       
        return vv;
    }

    queue<TreeNode*> q1;

};

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一码归—码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值