您的位置:首页 > 其它

<LeetCode OJ> Min Stack【155】

2015-12-30 19:17 330 查看


155. Min Stack

My Submissions

Question

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;
};


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: