716 最大栈

题目描述:
设计一个最大栈,支持 push、pop、top、peekMax 和 popMax 操作。
push(x) – 将元素 x 压入栈中。
pop() – 移除栈顶元素并返回这个值。
top() – 返回栈顶元素。
peekMax() – 返回栈中最大元素。
popMax() – 返回栈中最大的元素,并将其删除。如果有多个最大元素,只要删除最靠近栈顶的那个。

样例 1:
MaxStack stack = new MaxStack();
stack.push(5);
stack.push(1);
stack.push(5);
stack.top(); -> 5
stack.popMax(); -> 5
stack.top(); -> 1
stack.peekMax(); -> 5
stack.pop(); -> 1
stack.top(); -> 5

注释:
-1e7 <= x <= 1e7
操作次数不会超过 10000。
当栈为空的时候不会出现后四个操作。

方法1:
主要思路:
(1)使用两个栈实现,一个栈作为正常的存储数据元素的栈st,另一个只存储递增元素st_max,既只有当前要压入的元素大于等于之前的所有元素时,才进行压入到栈st_max中;
(2)但是后面处理pop和popMax时,要注意同时处理两个栈;

class MaxStack {
public:
    stack<int> st;
    stack<int> st_max;
    /** initialize your data structure here. */
    MaxStack() {

    }
    
    void push(int x) {
    	//将元素压入到栈中
        st.push(x);
        //根据情况,压入到最大栈st_max中
        if(st_max.empty())
            st_max.push(x);
        else if(x>=st_max.top())
            st_max.push(x);
    }
    
    int pop() {
    	//弹出栈中的元素
        int tmp=st.top();
        st.pop();
        //根据情况,弹出最大栈st_max中的元素
        if(tmp==st_max.top())
            st_max.pop();
        return tmp;
    }
    
    int top() {
        return st.top();
    }
    
    int peekMax() {
        return st_max.top();
    }
    
    int popMax() {
        int tmp=st_max.top();
        st_max.pop();//先获得当前的最大值
        stack<int>st_tmp;
        while(st.top()!=tmp){//将最大值前面的元素弹出,保存到临时的栈中
            st_tmp.push(st.top());
            st.pop();
        }
        st.pop();//弹出最大元素
        while(!st_tmp.empty()){//将临时栈中的元素压回到栈中
            int top_num=st_tmp.top();
            st.push(top_num);
            st_tmp.pop();
            //因为之前的最大值已经弹出,则最大栈st_max也可能同步的更新
            if(st_max.empty()){
                st_max.push(top_num);
            }
            else if(top_num>=st_max.top()){
                st_max.push(top_num);
            }
        }
        return tmp;//返回最大值
    }
};

/**
 * Your MaxStack object will be instantiated and called as such:
 * MaxStack* obj = new MaxStack();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->top();
 * int param_4 = obj->peekMax();
 * int param_5 = obj->popMax();
 */
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
单链表无法直接实现的数据结构,但是我们可以通过在单链表中记录当前的最大值来实现一个“最大”。 具体实现方法是,在定义单链表节点的结构体中增加一个max值,表示该节点所在链表中,以该节点为结尾的子链表的最大值。每次入时,将新节点的max值更新为当前节点与新节点值的最大值;每次出时,将前一个节点的max值更新为前一个节点与当前节点的max值的最大值。 这样,我们就可以通过访问单链表的尾节点的max值来获取当前最大值。 以下是一个简单的示例代码实现: ```C++ #include <iostream> using namespace std; struct Node { int val; // 节点值 int max; // 当前子链表的最大值 Node* next; // 指向下一个节点的指针 Node(int x) : val(x), max(x), next(nullptr) {} }; class MaxStack { private: Node* head; // 顶节点 public: MaxStack() : head(nullptr) {} void push(int x) { Node* node = new Node(x); if (head == nullptr) { head = node; } else { node->max = max(head->max, x); node->next = head; head = node; } } int pop() { if (head == nullptr) { return -1; // 为空 } else { int res = head->val; head = head->next; return res; } } int getMax() { if (head == nullptr) { return -1; // 为空 } else { return head->max; } } }; int main() { MaxStack s; s.push(5); s.push(1); s.push(3); cout << s.getMax() << endl; // 5 s.pop(); cout << s.getMax() << endl; // 5 s.pop(); cout << s.getMax() << endl; // 5 s.pop(); cout << s.getMax() << endl; // -1,为空 return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值