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();
}
}

最新文章

  1. bzoj 3223 splay模板题3
  2. POJ 2892 Tunnel Warfare
  3. SpringMVC+MyBatis项目分析与开发实例
  4. Js获取Cookie值的方法
  5. SGU 121.Bridges painting
  6. Linux 环境下自动化测试工具,Redhat dogtail的安装
  7. jQuery渐隐渐出的文字提示
  8. Facebook开源的基于SQL的操作系统检测和监控框架:osquery Table详解
  9. 安卓TV开发(八) 移动智能终端多媒体爬虫技术 获取加载网页视频源
  10. Docker使用Mysql镜像命令
  11. requests库入门05-参数类型
  12. 线程同步-Barrier类
  13. 转 -- Python: 多继承模式下 MRO(Method Resolution Order) 的计算方式关乎super
  14. vector at()函数比 []运算符操作安全
  15. sql 求max和min,但是第二大,第二小怎么算?
  16. laravel目录结构
  17. pdf阅读器改背景色
  18. jquery 问题
  19. 总结Codeigniter的一些优秀特性
  20. 【Linux】cp命令

热门文章

  1. oracle操作小计
  2. ImportError: No module named &#39;_sqlite3&#39;
  3. SPSS统计功能与模块对照表
  4. Unity中surfaceShader的处理机制和finalColor
  5. 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 &gt;= 7.0
  6. gridControl使用集锦
  7. 【BZOJ5016】[Snoi2017]一个简单的询问 莫队
  8. Convex optimization 凸优化
  9. Hadoop初体验
  10. SAP basis 二