实现stack 加上·getMin功能 时间复杂度为O(n)
2024-10-21 18:40:46
package com.hzins.suanfa; import java.util.Stack;
/**
* 实现stack 加上·getMin功能 时间复杂度为O(n)
* @author Administrator
*
*/
public class GetMinStack {
private Stack<Integer> stackData;
private Stack<Integer> stackMin;
public GetMinStack(){
this.stackData = new Stack<Integer>();
this.stackMin = new Stack<Integer>();
}
public void push(int newNum){
if(this.stackMin.isEmpty()){
stackMin.push(newNum);
}else if(newNum <this.getMin()){
stackMin.push(newNum);
}else{
stackMin.push(this.getMin());
}
stackData.push(newNum);
}
public int pop(){
if(stackData.isEmpty()){
throw new RuntimeException("Your stack is empty");
}
stackMin.pop();
return stackData.pop();
}
public int getMin(){
if(this.stackMin.isEmpty()){
throw new RuntimeException("Your stack is empty");
}
return this.stackMin.peek();
}
}
最新文章
- bzoj 3223 splay模板题3
- POJ 2892 Tunnel Warfare
- SpringMVC+MyBatis项目分析与开发实例
- Js获取Cookie值的方法
- SGU 121.Bridges painting
- Linux 环境下自动化测试工具,Redhat dogtail的安装
- jQuery渐隐渐出的文字提示
- Facebook开源的基于SQL的操作系统检测和监控框架:osquery Table详解
- 安卓TV开发(八) 移动智能终端多媒体爬虫技术 获取加载网页视频源
- Docker使用Mysql镜像命令
- requests库入门05-参数类型
- 线程同步-Barrier类
- 转 -- Python: 多继承模式下 MRO(Method Resolution Order) 的计算方式关乎super
- vector at()函数比 []运算符操作安全
- sql 求max和min,但是第二大,第二小怎么算?
- laravel目录结构
- pdf阅读器改背景色
- jquery 问题
- 总结Codeigniter的一些优秀特性
- 【Linux】cp命令
热门文章
- oracle操作小计
- ImportError: No module named &#39;_sqlite3&#39;
- SPSS统计功能与模块对照表
- Unity中surfaceShader的处理机制和finalColor
- error items-9022:missing required icon file.the bundle does not contain an app icon for iPhone/iPad Touch of exactly &#39;120x120&#39; pixels,in.pen format for ios versions >;= 7.0
- gridControl使用集锦
- 【BZOJ5016】[Snoi2017]一个简单的询问 莫队
- Convex optimization 凸优化
- Hadoop初体验
- SAP basis 二