您的位置:首页 > 其它

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

2016-09-24 19:52 176 查看
要求:

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压入稍费空间,但是弹出省时间。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: