Leetcode 155. Min Stack JAVA语言

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.

Example:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin();   --> Returns -3.
minStack.pop();
minStack.top();      --> Returns 0.
minStack.getMin();   --> Returns -2.

Subscribe to see which companies asked this question.

题意:设计一个有MIN函数的栈。

public class MinStack {
    private Stack<Integer> stackData;
    private Stack<Integer> stackMin;

    /** initialize your data structure here. */
    public MinStack() {
        stackData=new Stack<Integer>();
        stackMin=new Stack<Integer>();
    }
    
    public void push(int x) {
        if(stackMin.isEmpty()){
            stackMin.push(x);
        }else if(x<=stackMin.peek()){
            stackMin.push(x);
        }
        stackData.push(x);
    }
    /////这里貌似有点问题。。。。//包装类比较。。。。
    public void pop() {
        if(stackData.peek().equals(stackMin.peek())){
            stackMin.pop();
        }
        stackData.pop();
    }
    
    public int top() {
        return stackData.peek();
    }
    
    public int getMin() {
        return stackMin.peek();
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(x);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */

PS:定义一个辅助栈。用于存放每一步的最小值。参见左老师的书P1。

时间: 2024-10-08 22:32:34

Leetcode 155. Min Stack JAVA语言的相关文章

Java for 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

Java [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 min

LeetCode 155 Min Stack(最小栈)

翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检索栈的最小元素 原文 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop

[leetcode 155]min stack

1 题目 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 mini

Leetcode 155 Min Stack 小顶堆+栈,优先队列实现 难度:0

https://leetcode.com/problems/min-stack/ #include <vector> #include <queue> #include <map> #include <iostream> using namespace std; class MinStack { public: vector<int> vec; priority_queue<int,vector<int>,greater<

【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

一天一道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] 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

&amp;lt;LeetCode OJ&amp;gt; 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