【LeetCode】Min Stack 解题报告
2014-11-10 16:17
218 查看
【题目】
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.
【用Java内置的Stack实现】
来自:https://oj.leetcode.com/discuss/15659/simple-java-solution-using-two-build-in-stacks?state=edit-15691&show=15659#q15659
【分析】
这道题的关键之处就在于 minStack 的设计,push() pop() top() 这些操作Java内置的Stack都有,不必多说。
我最初想着再弄两个数组,分别记录每个元素的前一个比它大的和后一个比它小的,想复杂了。
第一次看上面的代码,还觉得它有问题,为啥只在 x<minStack.peek() 时压栈?如果,push(5), push(1), push(3) 这样minStack里不就只有5和1,这样pop()出1后, getMin() 不就得到5而不是3吗?其实这样想是错的,因为要想pop()出1之前,3就已经被pop()出了。.
minStack 记录的永远是当前所有元素中最小的,无论 minStack.peek() 在stack 中所处的位置。
【不用内置Stack的实现】
来自:https://oj.leetcode.com/discuss/15651/my-java-solution-without-build-in-stack
Python版解题报告:[LeetCode] Min Stack in Python
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.
【用Java内置的Stack实现】
来自:https://oj.leetcode.com/discuss/15659/simple-java-solution-using-two-build-in-stacks?state=edit-15691&show=15659#q15659
class MinStack { // stack: store the stack numbers private Stack<Integer> stack = new Stack<Integer>(); // minStack: store the current min values private Stack<Integer> minStack = new Stack<Integer>(); public void push(int x) { // store current min value into minStack if (minStack.isEmpty() || x <= minStack.peek()) minStack.push(x); stack.push(x); } public void pop() { // use equals to compare the value of two object, if equal, pop both of them if (stack.peek().equals(minStack.peek())) minStack.pop(); stack.pop(); } public int top() { return stack.peek(); } public int getMin() { return minStack.peek(); } }
【分析】
这道题的关键之处就在于 minStack 的设计,push() pop() top() 这些操作Java内置的Stack都有,不必多说。
我最初想着再弄两个数组,分别记录每个元素的前一个比它大的和后一个比它小的,想复杂了。
第一次看上面的代码,还觉得它有问题,为啥只在 x<minStack.peek() 时压栈?如果,push(5), push(1), push(3) 这样minStack里不就只有5和1,这样pop()出1后, getMin() 不就得到5而不是3吗?其实这样想是错的,因为要想pop()出1之前,3就已经被pop()出了。.
minStack 记录的永远是当前所有元素中最小的,无论 minStack.peek() 在stack 中所处的位置。
【不用内置Stack的实现】
来自:https://oj.leetcode.com/discuss/15651/my-java-solution-without-build-in-stack
class MinStack { Node top = null; public void push(int x) { if (top == null) { top = new Node(x); top.min = x; } else { Node temp = new Node(x); temp.next = top; top = temp; top.min = Math.min(top.next.min, x); } } public void pop() { top = top.next; return; } public int top() { return top == null ? 0 : top.val; } public int getMin() { return top == null ? 0 : top.min; } } class Node { int val; int min; Node next; public Node(int val) { this.val = val; } }
Python版解题报告:[LeetCode] Min Stack in Python
相关文章推荐
- 【LeetCode】Min Stack 解题报告
- LeetCode-Min Stack-解题报告
- 【LeetCode】Min Stack 解题报告
- [LeetCode]Min Stack,解题报告
- [LeetCode]Largest Rectangle in Histgram,解题报告
- LeetCode: Convert a Number to Hexadecimal 解题报告
- [leetcode] 285. Inorder Successor in BST 解题报告
- leetcode 383. Ransom Note 解题报告
- leetCode解题报告之Copy List with Random Pointer
- [Leetcode] 29. Divide Two Integers 解题报告
- LeetCode: Regular Expression Matching 解题报告
- LeetCode(33) Search in Rotated Sorted Array解题报告
- [Leetcode] 310. Minimum Height Trees 解题报告
- 【LeetCode】717. 1-bit and 2-bit Characters 解题报告
- 【LeetCode】298.Binary Tree Longest Consecutive Sequence(Medium)(加锁题)解题报告
- 【LeetCode】Binary Tree Level Order Traversal II 解题报告
- 【LeetCode】Counting Bits 解题报告
- LeetCode 216. Combination Sum III 解题报告
- LeetCode-Lowest Common Ancestor of a Binary Search Tree-解题报告
- [Leetcode] 31. Next Permutation 解题报告