Min Stack【155】

Datetime:2016-08-23 01:23:18          Topic: LeetCode           Share

155. Min Stack

My Submissions

Total Accepted:  55516 Total Submissions:  263927 Difficulty:  Easy

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.

Subscribe to see which companies asked this question

Hide Tags
Stack   Design

Hide Similar Problems

(H) Sliding Window Maximum

以下是错误答案, 有待以后调试,不舍得放弃,留了下来:

//思路首先:数组模拟栈
class MinStack {
public:
    MinStack()
    {
        arr.resize(100000);
        minarr.resize(100000);
        ntop=-1;
    }

    void push(int x) {
        ++ntop;
        arr[ntop]=x;
        if(ntop==0)
            minum=INT_MAX;
        if(x<=minum)
            minum=x;
        minarr[ntop]=minum;
    }

    void pop() {
        minarr[ntop]=0;
        ntop--;
    }

    int top() {
        return arr[ntop];
    }

    int getMin() {
        return minarr[ntop];
    }
private:
    vector<int> arr;
    vector<int> minarr;
    int ntop;
    int minum;
};





About List