题解 | #包含min函数的栈#

包含min函数的栈

https://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49

class Solution {
public:
	int *base=new int[300]; //栈底指针
	int *top1=base; //栈顶指针,切记要进行初始化
    stack<int> sta;
    void push(int value) {
        if (this->top1-this->base == 300) //栈满
        {
           return; 
        }
        if(this->top1==this->base)
        {
            *(this->top1++) = value;
            sta.push(value);
        }
        
	   *(this->top1++) = value; //元素e压入栈顶,然后栈顶指针加1,等价于*S.top=e; S.top++;
       sta.push(this->top()>sta.top()?sta.top():this->top() );
    }
    void pop() {
        if(this->top1==this->base)
        {
            return;
        }
        else
        {
            this->top1--;
            sta.pop();
        }
    }
    int top() {
        if(this->top1==this->base)
        {
            return 0;
        }
        else{
            return *(this->top1-1);
        }
    }
    int min() {
          return sta.top();
    }
};

全部评论

相关推荐

昨天 21:59
门头沟学院 Java
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 19:30
化身华黑&nbsp;今天询问对接人审批情况,结果被告知没HC了&nbsp;云计算&nbsp;
苦闷的柠檬精allin实习:主管面结束后hr每周保温一次,结果前几天和我说没hc了,我也化身华黑子了
投递华为等公司8个岗位 > 华为求职进展汇总
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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