包含min函数的栈
栈
题目
定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的 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.
提示:
各函数的调用总次数不超过 20000 次
注意:本题与主站 155 题相同:https://leetcode-cn.com/problems/min-stack/
Related Topics
栈
设计
👍 500
👎 0
class MinStack {
/** initialize your data structure here. */
public MinStack() {
}
public void push(int x) {
}
public void pop() {
}
public int top() {
}
public int 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();
*/
思路
- 看了解析后,使用双栈
- stackA存储所有元素
- stackB存储最小值
实现
import java.util.Stack;
//leetcode submit region begin(Prohibit modification and deletion)
class MinStack {
Stack<Integer> stackA;
Stack<Integer> stackB;
/** initialize your data structure here. */
public MinStack() {
stackA = new Stack<>();
stackB = new Stack<>();
}
public void push(int x) {
stackA.push(x);
if (stackB.empty() || stackB.peek()>=x) stackB.push(x);
}
public void pop() {
int r = stackA.pop();
if (!stackB.empty() && stackB.peek()==r) stackB.pop();
}
public int top() {
return stackA.peek();
}
public int min() {
return stackB.peek();
}
}
结果
解答成功:
执行耗时:14 ms,击败了34.47% 的Java用户
内存消耗:46 MB,击败了11.51% 的Java用户
链表
class MinStack {
class ListNode{
Integer val;
MinStack.ListNode pre;
MinStack.ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
}
MinStack.ListNode nodeA;
MinStack.ListNode nodeB;
/** initialize your data structure here. */
public MinStack() {
nodeA = new MinStack.ListNode();
nodeB = new MinStack.ListNode();
}
public void push(int x) {
MinStack.ListNode node = new MinStack.ListNode(x);
nodeA.next=node;
node.pre= nodeA;
nodeA = node;
if (nodeB.val==null || nodeB.val>=x) {
MinStack.ListNode node1 = new MinStack.ListNode(x);
nodeB.next=node1;
node1.pre= nodeB;
nodeB = node1;
}
}
public void pop() {
MinStack.ListNode node = nodeA;
if (nodeB.val!=null && nodeB.val.equals(node.val)) {
nodeB= nodeB.pre;
nodeB.next=null;
}
nodeA=nodeA.pre;
nodeA.next=null;
}
public int top() {
return nodeA.val;
}
public int min() {
return nodeB.val;
}
}
结果
解答成功:
执行耗时:12 ms,击败了99.34% 的Java用户
内存消耗:45.9 MB,击败了20.39% 的Java用户
数组
class MinStack {
int[] arrA;
int[] arrB;
int lengthA;
int lengthB;
/** initialize your data structure here. */
public MinStack() {
arrA = new int[10];
arrB = new int[10];
lengthA=0;
}
public void push(int x) {
arrA[lengthA]=x;
lengthA++;
int[] tempA = autoSize(arrA,lengthA);
if (tempA!=null){
arrA=tempA;
}
if (lengthB==0||arrB[lengthB-1]>=x){
arrB[lengthB]=x;
lengthB++;
int[] tempB = autoSize(arrB,lengthB);
if (tempB!=null){
arrB=tempB;
}
}
}
private int[] autoSize(int[] arr,int length){
if (length > arr.length*0.8){
int[] temp = arr;
arr = new int[temp.length*2];
for (int i=0;i<length;i++){
arr[i]=temp[i];
}
return arr;
}
return null;
}
public void pop() {
lengthA--;
if (lengthB!=0 && arrB[lengthB-1]==arrA[lengthA]){
lengthB--;
}
}
public int top() {
return arrA[lengthA-1];
}
public int min() {
return arrB[lengthB-1];
}
}
结果
解答成功:
执行耗时:15 ms,击败了19.04% 的Java用户
内存消耗:42.6 MB,击败了99.95% 的Java用户