设计一个有getMin功能的栈

实现一个特殊的栈,在实现栈的基本功能的基础上,再实现返回值的栈中最小元素的操作 要求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压入时稍微省空间,但是弹出的时候稍费时间,方案二压入时稍费空间,但弹出时稍省空间。这道题我感觉并不是特别难,唯一我觉得难的一点是用了两个栈来进行操作。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Master_Yoda

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值