[LeetCode] Min Stack Min Stack

Min Stack

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

  • push(x) -- Push element x onto stack.
  • pop() -- Removes the element on top of the stack.
  • top() -- Get the top element.
  • getMin() -- Retrieve the minimum element in the stack.

解题思路:

主要是获得当前最小值的问题。我们可以用一个动态数组min存储当前最小值。若新压入的元素大于动态数组min最后一个元素,不做任何操作。否则(小于或等于)就压入min中。出栈的时候,若出栈元素等于min最后一个元素,则min数组出栈。这样便实现了常量时间找到栈中的最小值了。下面是代码:

class MinStack {
public:
    MinStack(){
        capcity=2;
        data = new int[capcity];
        size=0;

        minCapcity=2;
        min = new int[minCapcity];
        minSize = 0;
    }
    ~MinStack(){
        delete[] data;
        delete[] min;
    }
    void push(int x) {
        if(size>=capcity){
            int* p=data;
            capcity = 2*capcity;
            data=new int[capcity];
            std::memcpy(data, p, sizeof(int)*size);
            delete[] p;
        }
        data[size++]=x;

        if(minSize==0){
            min[minSize++]=x;
        }else if(min[minSize-1]>=x){
            if(minSize>=minCapcity){
                int* p=min;
                minCapcity = 2*minCapcity;
                min = new int[minCapcity];
                std::memcpy(min, p, sizeof(int)*minSize);
                delete[] p;
            }
            min[minSize++]=x;
        }
    }

    void pop() {
        if(size>0){
            size--;
            if(data[size]==min[minSize-1]){
                minSize--;
            }
        }else{
            throw exception();
        }
    }

    int top() {
        if(size>0){
            return data[size-1];
        }else{
            throw exception();
        }
    }

    int getMin() {
        return min[minSize-1];
    }
private:
    int size;
    int capcity;
    int* min;
    int minSize;
    int minCapcity;
    int* data;
};

时间: 2024-08-07 00:11:06

[LeetCode] Min Stack Min Stack的相关文章

LeetCode[stack]: Min Stack

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. - push(x) – Push element x onto stack. - pop() – Removes the element on top of the stack. - top() – Get the top element. - getMin() – Retrieve the minim

LeetCode算法题-Min Stack(Java实现)

这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小元素的堆栈. push(x) - 将元素x推入堆栈. pop() - 删除堆栈顶部的元素. top() - 获取顶部元素. getMin() - 检索堆栈中的最小元素. 例如: MinStack minStack = new MinStack(); minStack.push(-2); minSta

【leetcode】155 - Min Stack

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve the minimum e

[LeetCode] 155. Min Stack_Easy tag: stack

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve the minimum e

【34】包含min函数的stack

题目: 实现一个包含min函数的栈,min和push,pop都是o(1)时间 思路: 采用一个辅助的栈,来存储不同阶段的最小值 - 代码: push(int value){ //data是数据栈,min是辅助栈用来存储最小值 data.push(value); if(min.size() < 0 || value < min.top()){ min.push(value); }else{ min.push(min.top()); } } pop(){ if(min.size() <= 0

【LeetCode 题解】Min Stack

同学推荐了LeetCode,相比其他的OJ,题量少,题目也比较经典,针对性特别强,适合练习. LeetCode相关的网上资源比较多,不过,看到题目一定要自己做一遍,然后再去参考其他人的解法. https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- P

【一天一道LeetCode】#155. Min Stack

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) – Push element x onto stack. pop() – Removes the el

LeetCode OJ:Min Stack(最小栈问题)

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve the minimum e

IT公司100题-2-设计带min函数的stack

问题描述: 定义栈的数据结构,要求添加一个min 函数,能够得到栈的最小元素. 要求函数min.push 以及pop 的时间复杂度都是O(1). 双倍空间实现: 保存2个栈,分别是元素和当前最小值. 压缩空间实现: ? 代码实现: package oschina.mianshi; /**  * @project: oschina  * @filename: IT2.java  * @version: 0.10  * @author: JM Han  * @date: 17:08 2015/10/