实现一个特殊的栈,在实现栈的基本功能的基础上,再实现返回值的栈中最小元素的操作 要求pop、push、getMin操作的时间复杂度都是O(1)
有两种设计方案:
第一种设计方案如下:
- 实现思路,在设计上我们使用两个栈,一个栈用来保存当前栈中的元素,这个栈记
- 为stackData,另一个栈用于保存每一步的最小值,这个栈几位stackMin 具体的实现方式有两种 第一种: 压入数据规则:
- 假设当前数据为newNum,先将其压入stackData,然后判断stackMin是否为空
- 如果为空newStack,则newNum也压入stackMin,如果不为空,则比较newNum和stackMin的栈顶元素哪个更小
- 如果newNum更小或者两者相等,则newNum也压入stackMin,否则不压入任何内容 弹出规则:
- 设弹出的元素记为value,如果value比stackMin中栈顶元素相等时,stackMin弹出元素,
- 如果value大于stackMin中的元素则不弹出
package com.dlj.stack.getMin;
import java.util.Stack;
public class MyStack1 {
private Stack<Integer> stackData;
private Stack<Integer> stackMin;
public MyStack1() {
stackData = new Stack<Integer>();
stackMin = new Stack<Integer>();
}
public void push(int newNum) {
// 先判断元素是否需要压入stackMmin,最后一步总是将元素压入要stackData中
if (this.stackMin.isEmpty()) {
this.stackMin.push(newNum);
} else if (newNum <= this.getMin()) {
this.stackMin.push(newNum);
}
this.stackData.push(newNum);
}
public int pop() {
if (this.stackData.isEmpty()) {
throw new RuntimeException("the stack is empty now!");
}
int value = this.stackData.pop();
if (value == this.getMin()) {
this.stackMin.pop();
}
return value;
}
public int getMin() {
if (this.stackMin.isEmpty()) {
throw new RuntimeException("the stack is empty now!");
}
return this.stackMin.peek();
}
public int peek() {
if (this.stackData.isEmpty()) {
throw new RuntimeException("the stack is empty now!");
}
return this.stackData.peek();
}
}
第二种设计方案:
压入数据规则
- 假设当前数据为newNum,先将其压入stackData,然后判断stackMin是否为空
- 如果为空,则newNum也压入stackMin,如果不为空,则比较newNum和stackMin的栈顶元素哪个更小
- 如果newNum更小或者两者相等,则newNum也压入stackMin,stackMin中栈顶元素更小,则把stackMin的栈顶元素重复压入stackMin,即在栈顶元素上在压入一个栈顶元素:
弹出数据规则
- 在stackData中弹出数据,弹出的数据记为value,同时也弹出stackMin中栈顶的元素,
package com.dlj.stack.getMin;
import java.util.Stack;
public class MyStack2 {
private Stack<Integer> stackData;
private Stack<Integer> stackMin;
public MyStack2() {
stackData = new Stack<Integer>();
stackMin = new Stack<Integer>();
}
public void push(int newNum) {
// 先判断元素是否需要压入stackMmin,最后一步总是将元素压入要stackData中
if (this.stackMin.isEmpty()) {
this.stackMin.push(newNum);
} else if (newNum <= this.getMin()) {
this.stackMin.push(newNum);
} else {
int newMin = this.stackMin.peek();
this.stackMin.push(newMin);
}
this.stackData.push(newNum);
}
public int pop() {
if (this.stackData.isEmpty()) {
throw new RuntimeException("the stack is empty now!");
}
this.stackMin.pop();
return this.stackData.pop();
}
public int getMin() {
if (this.stackMin.isEmpty()) {
throw new RuntimeException("the stack is empty now!");
}
return this.stackMin.peek();
}
public int peek() {
if (this.stackData.isEmpty()) {
throw new RuntimeException("the stack is empty now!");
}
return this.stackData.peek();
}
}
测试代码:
package com.dlj.stack.getMin;
public class GetMinStackMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyStack1 myStack1 = new MyStack1();
int[] arr = { 3, 4, 5, 1, 2, 1 };
for (int i = 0; i < arr.length; i++) {
myStack1.push(arr[i]);
System.out.print("The value to be pushed into the stack is:" + myStack1.peek());
System.out.println("The min value of the stack is:" + myStack1.getMin());
}
System.out.println("--------------");
MyStack1 myStack2 = new MyStack1();
for (int i = 0; i < arr.length; i++) {
myStack2.push(arr[i]);
System.out.print("The value to be pushed into the stack is:" + myStack2.peek());
System.out.println("The min value of the stack is:" + myStack2.getMin());
}
}
}
运行结果:
The value to be pushed into the stack is:3The min value of the stack is:3
The value to be pushed into the stack is:4The min value of the stack is:3
The value to be pushed into the stack is:5The min value of the stack is:3
The value to be pushed into the stack is:1The min value of the stack is:1
The value to be pushed into the stack is:2The min value of the stack is:1
The value to be pushed into the stack is:1The min value of the stack is:1
--------------
The value to be pushed into the stack is:3The min value of the stack is:3
The value to be pushed into the stack is:4The min value of the stack is:3
The value to be pushed into the stack is:5The min value of the stack is:3
The value to be pushed into the stack is:1The min value of the stack is:1
The value to be pushed into the stack is:2The min value of the stack is:1
The value to be pushed into the stack is:1The min value of the stack is:1
这两种方案都差不多,区别是方案一中stackMin压入时稍微省空间,但是弹出的时候稍费时间,方案二压入时稍费空间,但弹出时稍省空间。这道题我感觉并不是特别难,唯一我觉得难的一点是用了两个栈来进行操作。