栈和队列(1)——一个有getMin功能的栈

要求:

1.pop、push、getMin操作的时间复杂度都是O(1);

2.设计的栈类型可以使用现成的栈结构。


思考:

可以使用两个栈,一个用来保存当前栈中的元素,器其功能和一个正常的栈没区别,记为stackData;另一个用于保存每一步的最小值,这个栈记为stackMin。


方法一:

假设当前数据为newNum,先将其压入stackData,然后比较stackMin栈里面栈顶元素,如果stackMin是空的,就把newNum压入,如果不是,比较newNum和stackMin栈顶的元素,小就压入,大不不管。一次压入,把所有数据压完。

最后得到的stackMin的数据是从栈顶到栈底依次增大的,我们按照以下的数据规则将数据弹出:取stackData栈顶的元素,记为value,比较value和stackMin栈顶的元素。如果value等于stackMin栈顶的元素时,stackMin弹出栈顶元素;当value大于stackMin的栈顶元素时,stackMin不弹出栈顶元素;返回value。

仔细思考可得,stackMin始终记录着stackData里面最小值。所以stackMin的栈顶元素始终是当前stackData中的最小值。

例如:


代码实现:

<java>

package algorithm_1;
import java.util.*;
	public class MyStack1
	{
	public Stack<Integer> stackData;
	public Stack<Integer> stackMin;
	public MyStack1()
		{
			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);
		}
		this.stackData.push(newNum);
	}
	public int pop()
	{
		if(this.stackData.isEmpty())
		{
			throw new RuntimeException("Your stack is empty");
		}
		int value = this.stackData.pop();
		if(value == this.getmin())
		{
			this.stackMin.pop();
			System.out.println(value);
		}
		return value;
	}
	public int getmin()
	{
		if(this.stackMin.isEmpty())
		{
			throw new RuntimeException("Your stack is empty");
		}
		return this.stackMin.peek();
	}

	}

测试主函数:

package algorithm_1;
import algorithm_1.MyStack1;
public class algorithm_1 {
	    public static void main(String[] args){
	    	MyStack1 mytest ;
			mytest = new MyStack1();
			mytest.push(3);
			mytest.push(5);
			mytest.push(6);
			mytest.push(2);
			mytest.push(1);
			mytest.push(5);
			System.out.println("stackMin_in = ");
			System.out.println(mytest.stackMin);
			System.out.println("stackMin_out = ");
			for(int i = 0;i<6;i++)
				{
				 mytest.pop();
				
				}
		    }
	}
测试结果:

<C++>

方法二:

假设当前数据为newNum,将其压入stackData,然后判断stackMin是否为空。

如果为空则newNum压入stackMin;如果不为空,则比较newNum和stackMin的栈顶元素。如果newNum更小或者两者相等,则newNum也压入stackMin;如果stackMin中栈顶元素小,则把stackMin的栈顶元素重复压入stackMin。

在stackData中弹出数据,弹出的数据记为value;弹出的stackMin中的栈顶;返回value。stackMin始终几率这stackData中的最小值,所以stackMin的栈顶元素始终是当前stackData中的最小值。


代码实现:

<java>

package algorithm_1;
import java.util.*;
	public class MyStack2
	{
	public Stack<Integer> stackData;
	public Stack<Integer> stackMin;
	public MyStack2()
		{
			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);
		}
<span style="color:#ff0000;">		else
		{
			int newMin = this.stackMin.peek();
			this.stackMin.push(newMin);
		}
		this.stackData.push(newNum);</span>
	}
	public int pop()
	{
		if(this.stackData.isEmpty())
		{
			throw new RuntimeException("Your stack is empty");
		}
		
		System.out.println(this.stackMin.pop());
		return this.stackData.pop();
	}
	public int getmin()
	{
		if(this.stackMin.isEmpty())
		{
			throw new RuntimeException("Your stack is empty");
		}
		return this.stackMin.peek();
	}

	}


测试主函数:
package algorithm_1;
import algorithm_1.MyStack2;
public class algorithm_1 {
	    public static void main(String[] args){
	    	MyStack2 mytest ;
			mytest = new MyStack2();
			mytest.push(3);
			mytest.push(5);
			mytest.push(6);
			mytest.push(2);
			mytest.push(1);
			mytest.push(5);
			System.out.println("stackMin_in = ");
			System.out.println(mytest.stackMin);
			System.out.println("stackMin_out = ");
			for(int i = 0;i<6;i++)
				{
				 mytest.pop();
				
				}
			
	    }
	}

测试结果:



总结:

方法一和二其实都是用stackMin保存着stackData每一步的最小值。共同点是所有的时间复杂度都为O(1)、空间复杂度都为O(n)。区别是:方案一种的stackMin压入时省空间,但是弹出操作费时间;方案二stackMin压入稍费空间,但是弹出省时间。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值