设计一个有getMin功能的栈

【题目】实现一个特殊的栈,在实现栈的基本功能的基础上,再实现返回栈中最小元素的操作。
【要求】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压入时稍费空间,但是弹出操作稍省时间。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值