`

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.

实现一个可以返回最小值的堆栈,因为题目要求我们在O(1)的时间复杂度下返回最小值,我们需要用一个堆栈minStack单独来维护最小值,当进行push操作的时候,我们要检查压栈的元素是否是最小值,如果是最小值还要把这个元素放到minStack中去。pop操作的时候要检查出栈的元素是否为最小值,如果为最小值,在minStack中也要将这个元素弹出,getMin操作的时候只需要返回minStack的顶端的元素就可以了,期间不要忘记检查堆栈是否为空。代码如下:
class MinStack {
    Stack<Integer> stack = new Stack<Integer>();
    Stack<Integer> minStack = new Stack<Integer>();
    public void push(int x) {
        stack.push(x);
        if(!minStack.isEmpty()) {
            if(x <= minStack.peek()) {
                minStack.push(x);
            }
        } else {
            minStack.push(x);
        }
    }

    public void pop() {
        if(stack.isEmpty()) return;
        int num = stack.pop();
        if(num == minStack.peek()) {
            minStack.pop();
        }
    }

    public int top() {
        if(stack.isEmpty()) return Integer.MAX_VALUE;
        return stack.peek();
    }

    public int getMin() {
        if(minStack.isEmpty()) 
            return Integer.MAX_VALUE;
        return minStack.peek();
    }
}
分享到:
评论

相关推荐

    python-leetcode题解之155-Min-Stack.py

    - 当`min_stack`为空时,或者新元素小于或等于`min_stack`的栈顶元素时,将新元素入栈到`min_stack`。 - 当`min_stack`的栈顶元素大于新元素时,需要将新元素重复入栈,使得`min_stack`保持对最小值的追踪。 5. ...

    js-leetcode题解之155-min-stack.js

    在JavaScript中实现一个最小栈(Min Stack),是一个常见的算法问题,主要出现在LeetCode等编程练习平台上。在解决这类问题时,我们不仅要实现栈的基本功能,还要添加一个额外的功能:查询... if (this.minStack.length

    java-leetcode题解之155-Min-Stack

    public MinStack() { stack = new Stack(); } public void push(int x) { if (stack.isEmpty()) { stack.push(new int[]{x, x}); } else { int currentMin = stack.peek()[1]; stack.push(new int[]{x, ...

    java-leetcode面试题解Stack之第155题最小栈-题解.zip

    如果`minStack`为空或x小于`minStack`的栈顶元素,那么`x`也应成为`minStack`的栈顶元素。 3. 执行`pop()`时,两个栈都弹出顶部元素。 4. `top()`操作只需返回`mainStack`的栈顶元素。 5. `getMin()`操作则直接返回`...

    minstack:一个基于调试开发发布合一选型,及实训first方法论的全oslangapp最小化编程学习栈实现

    标题中的“minstack”指的是一个特别设计的编程学习栈,旨在提供一个轻量级的环境,用于初学者实践OS(操作系统)语言应用的开发。它强调了“调试、开发、发布合一”的选型,这意味着该栈包含了从编写代码、调试到...

    java面试-leetcode面试题解之第155题最小栈-题解.zip

    if (minStack.isEmpty() || x &lt;= minStack.peek()) { minStack.push(x); } } public void pop() { if (mainStack.pop().equals(minStack.peek())) { minStack.pop(); } } public int top() { return ...

    获得栈中的最小元素

    if (minStack.isEmpty() || x &lt;= minStack.peek()) { minStack.push(x); } } public void pop() { if (!mainStack.isEmpty()) { int popped = mainStack.pop(); if (popped.equals(minStack.peek())) { ...

    php-leetcode题解之最小栈.zip

    if (empty($this-&gt;minStack) || $x ($this-&gt;minStack)) { $this-&gt;minStack[] = $x; } } // 出栈操作 public function pop() { if (!empty($this-&gt;stack)) { if (end($this-&gt;stack) === end($this-&gt;minStack...

    数据结构+算法面试100题

    - `pop`操作时,检查被弹出的元素是否等于`minStack`栈顶元素,如果是,则`minStack`也弹出一个元素。 - `min`操作直接返回`minStack`的栈顶元素即可。 **代码示例**: ```cpp class MinStack { public: void push...

    微软等面试100题.

    void MinStackPush(MinStack& stack, int d) { if (stack.topData == stack.size) error("out of stack space."); stack.data[stack.topData].data = d; stack.data[stack.topData].min = (stack.topData == 0 ? ...

    微软公司等数据结构+算法面试100题(第1-100题)全部出炉

    1. **入栈操作**:当有新元素加入时,同时比较该元素与 `minStack` 的栈顶元素,取较小者入栈到 `minStack`。 2. **出栈操作**:出栈时同时弹出 `minStack` 的栈顶元素。 3. **获取最小值**:`minStack` 的栈顶元素...

    微软数据结构算法面试100题及解答

    这段代码实现了一个特殊的栈`MinStack`,在每次压栈时都会记录到目前为止的最小值,这样在任何时候都能够以O(1)的时间复杂度获取到栈中的最小元素。 ### 总结 以上两个题目是微软面试中较为典型的数据结构与算法...

    微软等数据结构+算法面试100题全部答案集锦

    void MinStackPush(MinStack *stack, int d) { if (stack-&gt;top == stack-&gt;size) error("Out of stack space."); MinStackElement *p = &(stack-&gt;data[stack-&gt;top]); p-&gt;data = d; p-&gt;min = (stack-&gt;top == 0 ? d...

    [最新整理公布][汇总II]微软等数据结构+算法面试100题[第1-80题]

    if (minStack.empty() || x &lt;= minStack.top()) { minStack.push(x); } } void pop() { if (dataStack.top() == minStack.top()) { minStack.pop(); } dataStack.pop(); } int top() { return data...

    百度质量部面试试题

    if (minStack.isEmpty() || x &lt;= minStack.peek()) { minStack.push(x); } mainStack.push(x); } public int pop() { int x = mainStack.pop(); if (x == minStack.peek()) { minStack.pop(); } return ...

    最新整理]微软等公司数据结构+算法面试100题[第1-80题]

    - 每当向 `dataStack` 中压入新元素时,同时比较该元素与 `minStack` 顶部的元素,取较小者压入 `minStack`。 - 当从 `dataStack` 中弹出元素时,也需要从 `minStack` 中弹出相应的元素。 - **代码实现**(示例)...

    微软数据结构+算法+面试

    if (minStack.empty() || x &lt;= minStack.top()) { minStack.push(x); } } void pop() { if (dataStack.top() == minStack.top()) { minStack.pop(); } dataStack.pop(); } int top() { return data...

    leetcode包含min函数的栈,python全网时间内存最小解法 代码+思路

    MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.min(); --&gt; 返回 -3. minStack.pop(); minStack.top(); --&gt; 返回 0. minStack.min(); --&gt; 返回 -...

    stack-with-constant-min-js:可以以O(1)时间复杂度返回其MIN元素的堆栈

    其次,如果推送的元素低于minStack的top元素,则将其推送到minStack,否则我们将克隆minStack的top元素。 当我们弹出一个元素时,我们会同时从两个堆栈中弹出它,以使两个堆栈保持同步。 这样,我们总是可以通过弹...

Global site tag (gtag.js) - Google Analytics