【问题描述】[中等]
定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的 min 函数在该栈中,调用 min、push 及 pop 的时间复杂度都是 O(1)。示例:MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.min(); --> 返回 -3.
minStack.pop();
minStack.top(); --> 返回 0.
minStack.min(); --> 返回 -2.
【解答思路】
1. 单栈
时间复杂度:O(N^2) 空间复杂度:O(1)
class MinStack {private LinkedList<Integer> data;private int min;/** initialize your data structure here. */public MinStack() {data = new LinkedList<Integer>();min = Integer.MAX_VALUE;}public void push(int x) {if(x <= min){data.add(min);min = x;}data.add(x);}public void pop() {if(data.pollLast() == min){//flush the minmin = data.pollLast();}}public int top() {return data.peekLast();}public int min() {return min;}
}/*** 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.min();*/
Stack
class MinStack {private Stack<Integer> stack;private int min;/** initialize your data structure here. */public MinStack() {stack = new Stack<>();min = Integer.MAX_VALUE;}public void push(int x) {if(x <= min ){//注意:这里要使用<=号stack.push(min);//在每一个min入栈之前将它前一个min入栈min = x;}stack.push(x);}public void pop() {if(stack.pop() == min){//如果取出来的是当前min,就将压在它低下的前一个min出栈min = stack.pop();}}public int top() {return stack.peek();}public int min() {return min;}
}
2. 双栈(辅助栈)
时间复杂度:O(1) 空间复杂度:O(N)
class MinStack {Stack<Integer> stack;Stack<Integer> minStack;/** initialize your data structure here. */public MinStack() {stack = new Stack<>();minStack = new Stack<>();minStack.push(Integer.MAX_VALUE);}public void push(int x) {stack.push(x);if(x <= minStack.peek()){minStack.push(x);}}public void pop() {int min = minStack.peek();if(stack.peek() == min){minStack.pop();}stack.pop();}public int top() {return stack.peek();}public int min() {return minStack.peek();}
}/*** 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.min();*/
class MinStack {Stack<Integer> A, B;public MinStack() {A = new Stack<>();B = new Stack<>();}public void push(int x) {A.add(x);if(B.empty() || B.peek() >= x)B.add(x);}public void pop() {if(A.pop().equals(B.peek()))B.pop();}public int top() {return A.peek();}public int min() {return B.peek();}
}
【总结】
1.Stack 的常用方法:
push( num) 入栈
pop() 栈顶元素出栈
empty() 判定栈是否为空
peek() 获取栈顶元素
search(num) 判端元素num是否在栈中,如果在返回1,不在返回-1。 注意pop()和peek()的区别。pop()会弹出栈顶元素并返回栈顶的值,peek()只是获取栈顶的值,但是并不会把元素从栈顶弹出来
2.Queue
Queue是在两端出入的List,所以也可以用数组或链表来实现。
- add 增加一个元索 如果队列已满,则抛出一个IIIegaISlabEepeplian异常
- remove 移除并返回队列头部的元素 如果队列为空,则抛出一个NoSuchElementException异常
- element 返回队列头部的元素 如果队列为空,则抛出一个NoSuchElementException异常
- offer 添加一个元素并返回true 如果队列已满,则返回false
- poll 移除并返问队列头部的元素 如果队列为空,则返回null
- peek 返回队列头部的元素 如果队列为空,则返回null
- put 添加一个元素 如果队列满,则阻塞
- take 移除并返回队列头部的元素 如果队列为空,则阻塞
注意
-
remove、element、offer 、poll、peek 其实是属于Queue接口。
-
add remove element操作在队满或者队空的时候会报异常。
-
offer poll peek 在队满或者队空的时候不会报异常。
-
put take操作属于阻塞操作。队满队空均会阻塞。
3.LinkedList
- 以双向链表实现的LinkedList既是List,也是Queue。
- 它是唯一一个允许放入null的Queue。
4. 单栈双栈思想均要掌握 面试算法常考题
参考链接:https://leetcode-cn.com/problems/bao-han-minhan-shu-de-zhan-lcof/solution/mian-shi-ti-30-bao-han-minhan-shu-de-zhan-fu-zhu-z/
参考链接:https://leetcode-cn.com/problems/bao-han-minhan-shu-de-zhan-lcof/solution/java-jian-ji-wu-fu-zhu-zhan-by-rabbitzhao/
参考链接:https://leetcode-cn.com/problems/bao-han-minhan-shu-de-zhan-lcof/solution/shuang-zhan-wei-hu-jie-fa-ji-linkedlistjie-fa-by-c/