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; } }
相关推荐
javascript js_leetcode题解之155-min-stack.js
java入门 java_leetcode题解之155_Min_Stack
python python_leetcode题解之155_Min_Stack.py
如果`minStack`为空或x小于`minStack`的栈顶元素,那么`x`也应成为`minStack`的栈顶元素。 3. 执行`pop()`时,两个栈都弹出顶部元素。 4. `top()`操作只需返回`mainStack`的栈顶元素。 5. `getMin()`操作则直接返回`...
leetcode 分类 Leetcode代码题解 随缘刷题,选择性更新题解 LeetCode 94 144 145 二叉树的前中后序遍历: Leetcode ...longest-consecutive-sequence ...155 Min Stack 最小栈: LeetCode 773 滑动谜题: ......
以155.Min Stack2为例: . └── 155.Min Stack2 #LeetCode第150题 ├── JavaScript / TypeScript for LeetCode (五十九).md #存放此题博客文档 ├── 155.Min Stack2.js #存放此题JavaScript Solution └─...
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 ...
minStack stack 111 二叉树的最小深度 , tree 110 平衡二叉树 tree 104 二叉树的最大深度 tree 100 相同的树 tree 94 二叉树的中序遍历 tree 70 爬楼梯 dp 53 最大子序和 dp 26 删除排序数组中的重复项 array 1 两数...
155.Min Stack 设置一个辅助栈专门用于放置当前最小的值,每次压栈时主栈压入新数据,辅助栈压入当前最小数据 316.Remove 重要 单调栈的问题,栈内元素大致递增,记录下串内字符的数量。有四题大致相同,总结一下...
if (minStack.isEmpty() || x <= minStack.peek()) { minStack.push(x); } } public void pop() { if (mainStack.pop().equals(minStack.peek())) { minStack.pop(); } } public int top() { return ...
* [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue](https://github.com/kamyu104/LeetCode#queue) * [Heap](https://github.com/kamyu104/LeetCode#heap) * [Tree]...
leetcode分发糖果 Leetcode C++ Solution Don't try to understand ...155-最小栈:min-stack Tree 普通二叉树 94-二叉树中序遍历:inorder-traversal 100-相同的二叉树:same-tree 101-对称二叉树:symmetric-
min-stack design-circular-deque trapping-rain-water 滑动窗口 通过窗口的大小不断调整来找到合适的值,或者窗口大小不变,通过左右移动来找到相应的结果 \ \ 其他 非 LeetCode 题,单纯在别人面试中看到的 \ 链表...
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...