/*
设计一个有getMin功能的栈
【题目】
实现一个特殊的栈,在实现栈的基本功能的基础上,再实现返回
栈中最小元素的操作。
【要求】
1.pop、push、getMin操作的时间复杂度都是O(1)。
2.设计的栈类型可以使用现成的栈结构。
在设计上我们使用两个栈,一个栈用来保存当前栈中的元素,其
功能和一个正常的栈没有区别,这个栈记为stackData;另一个栈用
于保存每一步的最小值,这个栈记为stackMin。具体的实现方式有两
种
*/
public class C01_GetMinStack {
//方法1:如果栈为空,压入数据,如果不为空并且比栈顶数值小,压入栈,否则不处理
public static class MyStack1{
private Stack<Integer>data;//记录全部数的栈
private Stack<Integer>min;//几录最小值的栈
public MyStack1(){//构造方法new 栈
this.data = new Stack<Integer>();
this.min = new Stack<Integer>();
}
public void push(int num){//压栈
if(min.isEmpty()){
min.push(num);
}else if(num<=getMin()){
min.push(num);
}
data.push(num);
}
public int pop(){//出栈
if(data.isEmpty()){//栈为空就抛出一个运行时异常
throw new RuntimeException("your stack is empty");
}
int value = data.pop();
if(value==getMin()){//相等时出栈
min.pop();
}
return value;
}
public int getMin(){
if(min.isEmpty()){
throw new RuntimeException("your stack is empty");
}
return min.peek();
}
}
//方法2:min为空或者num大于栈顶直接压入,num小于等于min的栈顶就压入num
public static class MyStack2{
private Stack<Integer>data;
private Stack<Integer>min;
public MyStack2(){
this.data = new Stack<Integer>();
this.min = new Stack<Integer>();
}
public void push(int num){
if(min.isEmpty() || num<=getMin()){
min.push(num);
}else if(num<=getMin()){
min.push(getMin());
}
data.push(num);
}
public int pop(){
int value = data.pop();
min.pop();
return value;
}
public int getMin(){
if(min.isEmpty()){
throw new RuntimeException("your stack is empty");
}
return min.peek();
}
}
public static void main(String[] args) {
MyStack1 stack1 = new MyStack1();//2 2 0 0 2
stack1.push(2);
System.out.println(stack1.getMin());
stack1.push(5);
System.out.println(stack1.getMin());
stack1.push(0);
System.out.println(stack1.getMin());
System.out.println(stack1.pop());
System.out.println(stack1.getMin());
System.out.println("---------------");
MyStack2 stack2 = new MyStack2();//2 2 0 0 2
stack2.push(2);
System.out.println(stack2.getMin());
stack2.push(5);
System.out.println(stack2.getMin());
stack2.push(0);
System.out.println(stack2.getMin());
System.out.println(stack2.pop());
System.out.println(stack2.getMin());
}
}
//方案一和方案二其实都是用stackMin栈保存着stackData每一步的
//最小值。共同点是所有操作的时间复杂度都为O(1)、空间复杂度都为
//O(n)。区别是:方栗一中stackMin压入时稍省空间,但是弹出操作稍
//费时间;方案二中stackMin压入时稍费空间,但是弹出操作稍省时间。
程序员代码面试指南--设计一个具有getMin功能的栈
原创
©著作权归作者所有:来自51CTO博客作者wx62eb7326068f3的原创作品,请联系作者获取转载授权,否则将追究法律责任
下一篇:左神算法课堂系列--纸条折痕问题
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
FittenChat:程序员写代码的最好辅助利器,没有之一
FittenChat:程序员写代码的最好辅助利器,没有之一
Code 插入图片 自动生成 -
【栈与队列_1】设计一个有getMin功能的栈
实现自己梦想的最好方式就是学习~目录【题目】【要求】【难度】【解答】1. 压入数据规则2. 弹出数据规则
队列 数据结构 java Data 数据 -
程序员代码面试指南--如何仅用递归函数和栈操作逆序一个栈
/** 如何仅用递归函数和栈操作逆序一个栈 【题目】 一个栈依次压入1、2、3、4、5,那么从栈顶到栈底分别为5、4、 3
Java 逆序栈 左神 程序员面试指南 Stack -
程序员代码面试指南--猫狗队列
/** 猫狗队列 【题目】 实现一种狗猫队列的结构,要求如下: 用户可以调用add方法将cat类或dog类的实例放入队列中; 用户可以调
Java 算法 猫狗队列 程序员面试指南 java -
【面试招聘】程序员面试完全指南
春季是求职的黄金时期,借这时机分享下程序员面试相关的感悟。本文立意不仅于面试技巧,而贵在常包括:简历...
算法 面试 编程语言 人工智能 java -
python 将numpy2降级
目前selenium版本已经升级到3.0了,网上的大部分教程是基于2.0写的,所以在学习前先要弄清楚版本号,这点非常重要。本系列依然以selenium2为基础,目前selenium3坑比较多,暂时没精力去研究,后续会出相关教程。 一、selenium简介 Selenium
python 将numpy2降级 环境变量 Selenium easy_install