【题目】实现一个特殊的栈,在实现栈的基本功能的基础上,再实现返回栈中最小元素的操作。
【要求】1.pop、push、getMin操作的时间复杂度都是O(1)。2.设计的栈类型可以使用现成的栈结构。
pop:删除此堆栈顶部的元素,并将该值作为此函数的值返回(Vector 对象中的最后一项)。 peek:查看栈顶元素,不删除它。
push:将元素项压入栈顶部。调用了Vector的addElement方法。该方法将指定的组件添加到此向量的末尾,将其大小增加1。
如果该载体的大小大于其容量,则该载体的容量增加。源码如下:
public E push(E item) {
addElement(item);
return item;
}
public synchronized void addElement(E obj) {
modCount++;
add(obj, elementData, elementCount);
}
该方法的功能与add(E)方法相同(它是List接口的一部分)。但是add返回布尔类型 而push则返回插入元素的类型。*
思路一:
*压入规则
假设当前数据是newNum,现将其压入栈stackData,判断辅助栈stackMin是否为空:
1.辅助栈为空,把newNum压入stackMin;
2.不为空,比较newNum与辅助栈顶元素比哪一个更小:若newNum小于等于辅助栈顶元素,则将其压入辅助栈。否则不对辅助栈做任何操作。
例如:压入3、4、5、1、2、1的过程中:
*弹出规则
先从stackData中弹出栈顶元素value,比较与辅助栈栈顶元素谁大谁小。由压入规则可知,辅助栈顶元素一定小于等于value。当value等于辅助栈栈顶元素时,stackMin弹出栈顶元素,否则辅助栈不做任何操作。返回StackData中的value。
*查询栈中最小值
压入弹出规则对应,辅助栈始终记录着stackData中的最小值,故辅助栈栈顶元素始终是当前stackData中的最小值。
代码实现如下:
import java.util.Stack;
public class getMinStack {
public static class Mystack{
private Stack<Integer>stackData;
private Stack<Integer>stackMin;
public Mystack() {
this.stackData = new Stack<Integer>();
this.stackMin = new Stack<Integer>();
}
public void push(int newNum) {
if(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!");
}
this.stackMin.pop();
return this.stackData.pop();
}
public int getMin() {
if(this.stackMin.isEmpty()) {
throw new RuntimeException("The stack is empty!");
}
return this.stackMin.peek();
}
}
public static void main(String []args) {
Mystack stack1 = new Mystack();
//测试过一组用例没问题,换了不同的元素进行测试
stack1.push(3);
System.out.println(stack1.getMin());
System.out.println("-------------------");
stack1.push(4);
stack1.push(5);
System.out.println(stack1.getMin());
System.out.println("-------------------");
stack1.push(1);
System.out.println(stack1.getMin());
System.out.println("-------------------");
stack1.pop();
System.out.println(stack1.getMin());
System.out.println("-------------------");
stack1.push(2);
System.out.println(stack1.getMin());
System.out.println("-------------------");
stack1.push(7);
System.out.println(stack1.getMin());
}
}
结果如下:
思路二:
*压入规则
假设当前数据为newNum,现将其压入栈stackData,然后判断辅助栈stackMin是否为空。
1.辅助栈为空,newNum压入stackData;
2.辅助栈不为空,比较newNum与辅助栈栈顶元素哪个更小;若newNum小于等于辅助栈栈顶元素,就将其压入辅助栈;如果辅助栈元素 小,则将其栈顶元素重复压入辅助栈。
依次压入3、4、5、1、2、1,其过程如下:
*弹出规则
与压入规则对应即可。
*查询当前栈中最小值
stackMin始终记录着stackData中的最小值,所以stackMin的栈顶元素始终是当前stackData中的最小值。
import java.util.Stack;
import zuochengyun.getMinStack.Mystack;
public class getMinStack1 {
public static class Mystack{
private Stack<Integer> stackData;
private Stack<Integer> stackMin;
public Mystack() {
this.stackData = new Stack<Integer>();
this.stackMin = new Stack<Integer>();
}
public void push(int newNum) {
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");
}
this.stackMin.pop();
return this.stackData.pop();
}
public int getMin() {
if(this.stackMin.isEmpty()) {
throw new RuntimeException("The stack is empty!");
}
return this.stackMin.peek();
}
}
public static void main(String []args) {
Mystack stack1 = new Mystack();
//测试过一组用例没问题,换了不同的元素进行测试
stack1.push(3);
System.out.println(stack1.getMin());
System.out.println("-------------------");
stack1.push(4);
stack1.push(5);
System.out.println(stack1.getMin());
System.out.println("-------------------");
stack1.push(1);
System.out.println(stack1.getMin());
System.out.println("-------------------");
stack1.pop();
System.out.println(stack1.getMin());
System.out.println("-------------------");
stack1.push(2);
System.out.println(stack1.getMin());
System.out.println("-------------------");
stack1.push(7);
System.out.println(stack1.getMin());
System.out.println("-------------------");
stack1.pop();
stack1.pop();
stack1.pop();
stack1.pop();
// stack1.pop();
System.out.println(stack1.getMin());
System.out.println("-------------------");
stack1.pop();
// System.out.println(stack1.pop());
}
}
结果:
总结
思路一中stackMin压入时稍省空间,但是弹出操作稍费时间;思路二中stackMin压入时稍费空间,但是弹出操作稍省时间。