`

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 element in the stack.

Solution 1:

用两个Stack。

class MinStack {
    private Stack<Integer> stack = new Stack<>();
    private Stack<Integer> minStack = new Stack<>();
    
    public void push(int x) {
        stack.push(x);
        if(minStack.isEmpty() || x <= minStack.peek()) {
            minStack.push(x);
        }
    }

    public void pop() {
        int x = stack.pop();
        if(x == minStack.peek()) {
            minStack.pop();
        }
    }

    public int top() {
        return stack.peek();
    }

    public int getMin() {
        return minStack.peek();
    }
}

 

Solution 2:

用一个Stack。

class MinStack {
    private Stack<Long> s = new Stack<Long>();
    private long min;
    
    public void push(int x) {
        if(s.isEmpty()) {
            s.push(0L);
            min = x;
        } else {
            s.push(x-min);
            if(x<min) min=x;
        }
    }

    public void pop() {
        if(s.isEmpty()) return;
        long value = s.pop();
        if(value < 0) {
            min = min - value;
        }
    }

    public int top() {
        long top = s.peek();
        if(top < 0) {
            return (int)min;
        } else {
            return (int)(min + top);
        }
    }

    public int getMin() {
        return (int)min;
    }
}

 

分享到:
评论

相关推荐

    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, ...

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

    155题是LeetCode上的一道中等难度的算法题目,主要涉及到设计和实现一个最小栈(Min Stack),要求这个栈除了具备普通栈的基本操作以外,还需要能够返回当前栈中的最小值。 最小栈的实现可以通过多种方法来完成,...

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

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

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

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

    leetcode分类-Leetcode:Leetcode题解

    leetcode 分类 Leetcode代码题解 随缘刷题,选择性更新题解 LeetCode 94 144 145 二叉树的前中后序遍历: Leetcode ...longest-consecutive-sequence ...155 Min Stack 最小栈: LeetCode 773 滑动谜题: ......

    leetcode题库-JavaScript-or-TypeScript-for-LeetCode:使用JavaScript或TypeScrip

    ​ 以155.Min Stack2为例: . └── 155.Min Stack2 #LeetCode第150题 ├── JavaScript / TypeScript for LeetCode (五十九).md #存放此题博客文档 ├── 155.Min Stack2.js #存放此题JavaScript Solution └─...

    看leetcode吃力-my-learning-note:大三下学期之资料结构与演算法课程内容与练习

    155. Min Stack 完成LeetCode 232. Implement Queue using Stacks Week5 完成LeetCode 147. Insertion Sort List 完成作业Quick sort Week6 10/11 国庆弹性放假 Week7 完成作业Heap sort Week8 完成作业Merge sort ...

    leetcode跳跃-algorithm:leetcode

    minStack stack 111 二叉树的最小深度 , tree 110 平衡二叉树 tree 104 二叉树的最大深度 tree 100 相同的树 tree 94 二叉树的中序遍历 tree 70 爬楼梯 dp 53 最大子序和 dp 26 删除排序数组中的重复项 array 1 两数...

    leetcode316-Every_day_leetcode:Every_day_leetcode

    155.Min Stack 设置一个辅助栈专门用于放置当前最小的值,每次压栈时主栈压入新数据,辅助栈压入当前最小数据 316.Remove 重要 单调栈的问题,栈内元素大致递增,记录下串内字符的数量。有四题大致相同,总结一下...

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

    LeetCode最全代码

    * [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue](https://github.com/kamyu104/LeetCode#queue) * [Heap](https://github.com/kamyu104/LeetCode#heap) * [Tree]...

    leetcode分发糖果-ForDaFactory:使用C++的个人leetcode解决方案

    leetcode分发糖果 Leetcode C++ Solution Don't try to understand ...155-最小栈:min-stack Tree 普通二叉树 94-二叉树中序遍历:inorder-traversal 100-相同的二叉树:same-tree 101-对称二叉树:symmetric-

    leetcode添加元素使和等于-leetcode:力码

    min-stack design-circular-deque trapping-rain-water 滑动窗口 通过窗口的大小不断调整来找到合适的值,或者窗口大小不变,通过左右移动来找到相应的结果 \ \ 其他 非 LeetCode 题,单纯在别人面试中看到的 \ 链表...

    java二叉树算法源码-algorithm-primer:algorithmprimer-算法基础、Leetcode编程和剑指offer,Ja

    155 最小栈 Min Stack Easy 394 字符串编码 Decode String Medium 链表 No Problem Solution Difficulty Tag 2 两数相加 Add Two Numbers Medium 19 删除链表倒数第N个节点 Remove Nth Node From End of List Medium...

Global site tag (gtag.js) - Google Analytics