[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. to

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.
public class _155MinStack2 {

    private Stack<Integer> stack;
    private Stack<Integer> minStack;

    /** initialize your data structure here. */
    public _155MinStack2() {
        stack = new Stack<Integer>();
        minStack = new Stack<Integer>();
    }

    public void push(int x) {
        stack.push(x);
        if (minStack.isEmpty()) {
            minStack.push(x);
        } else {
            if (minStack.peek() >= x)
                minStack.push(x);
        }
    }

    public void pop() {
        if (!stack.isEmpty()) {
            //判断对象相等用equals
            if (stack.peek().equals(minStack.peek())) {
                minStack.pop();
            }
            stack.pop();
        }
    }

    public int top() {
        if (!stack.isEmpty())
            return stack.peek();
        return 0;
    }

    public int getMin() {
        if (!minStack.isEmpty())
            return minStack.peek();
        return 0;
    }

    /**
     * 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();
     */
目录
相关文章
|
算法 安全 Java
【数据结构与算法】6、栈(Stack)的实现、LeetCode:有效的括号
【数据结构与算法】6、栈(Stack)的实现、LeetCode:有效的括号
99 0
|
存储 算法 测试技术
从小白开始刷算法 Stack 栈篇 leetcode.496
从小白开始刷算法 Stack 栈篇 leetcode.496
101 0
|
算法
从小白开始刷算法 Stack 栈篇 leetcode.20
从小白开始刷算法 Stack 栈篇 leetcode.20
171 0
LeetCode 225. Implement Stack using Queues
使用队列实现栈的下列操作: push(x) -- 元素 x 入栈; pop() -- 移除栈顶元素; top() -- 获取栈顶元素; empty() -- 返回栈是否为空
150 0
LeetCode 225. Implement Stack using Queues
|
存储
图解LeetCode——剑指 Offer 30. 包含min函数的栈
图解LeetCode——剑指 Offer 30. 包含min函数的栈
130 0
|
Swift
LeetCode 剑指 Offer 30. 包含min函数的栈(swift)
定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的 min 函数在该栈中,调用 min、push 及 pop 的时间复杂度都是 O(1)。
189 0
Leetcode-Easy 155. Min Stack
Leetcode-Easy 155. Min Stack
153 0
Leetcode-Easy 155. Min Stack
|
算法 前端开发 程序员
「LeetCode」剑指Offer-30包含min函数的栈⚡️
「LeetCode」剑指Offer-30包含min函数的栈⚡️
163 0
「LeetCode」剑指Offer-30包含min函数的栈⚡️
LeetCode 225:用队列实现栈 Implement Stack using Queues
题目: 使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 Implement the following operations of a stack using queues.
1120 0
LeetCode 155:最小栈 Min Stack
LeetCode 155:最小栈 Min Stack 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。 push(x) -- 将元素 x 推入栈中。 pop() -- 删除栈顶的元素。
863 0

热门文章

最新文章