用两个stack, 第一个按顺序放所有值,第二个只放当前最小值。

注意: 1. 最小值有多个则都放到两个stack里, 尤其别忘放第二个; 2. pop时若两个stack的最上面值相等则都pop, 不等则只pop第一个stack, 但是都得返回第一个stack的pop值; 3. min时只返回第二个stack的peek值。

  1. public class MinStack {
  2. Stack<Integer> stack1 = new Stack<Integer>();
  3. Stack<Integer> stack2 = new Stack<Integer>();
  4.  
  5. public MinStack() {
  6. // do initialize if necessary
  7. }
  8.  
  9. public void push(int number) {
  10. stack1.push(number);
  11. if(stack2.empty()){
  12. stack2.push(number);
  13. } else{
  14. if(stack2.peek() >= number){
  15. stack2.push(number);
  16. }
  17. }
  18. // write your code here
  19. }
  20.  
  21. public int pop() {
  22. if(stack1.empty() || stack2.empty()){
  23. return -1;
  24. }
  25.  
  26. if(stack1.peek().equals(stack2.peek())){
  27. stack2.pop();
  28. return stack1.pop();
  29. } else{
  30. return stack1.pop();
  31. }
  32. // write your code here
  33. }
  34.  
  35. public int min() {
  36. return stack2.peek();// write your code here
  37. }
  38. }

LintCode Min Stack的更多相关文章

  1. [LintCode] Min Stack 最小栈

    Implement a stack with min() function, which will return the smallest number in the stack. It should ...

  2. [CareerCup] 3.2 Min Stack 最小栈

    3.2 How would you design a stack which, in addition to push and pop, also has a function min which r ...

  3. leetcode 155. Min Stack --------- java

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  4. Min Stack [LeetCode 155]

    1- 问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant ...

  5. Min Stack

    Min Stack Design a stack that supports push, pop, top, and retrieving the minimum element in constan ...

  6. Java [Leetcode 155]Min Stack

    题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...

  7. 155. Min Stack

    题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...

  8. leetCode Min Stack解决共享

    原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...

  9. LeetCode算法题-Min Stack(Java实现)

    这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小 ...

随机推荐

  1. cc1101 ASK发射模式

    cc1101 配置  433.919830Mhz  1.19948kBaud   199.951172  58.035714 #ifndef _CC1100_H_#define _CC1100_H_ ...

  2. 导航效果css

    <!doctype html> <html> <head> <meta charset="utf-8" /> <style&g ...

  3. React Native + Nodejs 使用RSA加密登录

    想用rn做个RSA(非对称加密)登录 基本流程就是在服务端生成RSA后,将“公钥”发到客户端,然后客户端用“公钥”加密信息发送到服务端,服务务端用私钥解密. 过程不复杂,问题在于,nodejs和rn都 ...

  4. centos 6.6编译安装git-2.7.0 最新git编译安装

    系统环境:centos6.6 1,安装准备 yum -y install gcc zlib-devel openssl-devel perl cpio expat-devel gettext-deve ...

  5. 初学js

    接触时感觉跟前面写网页的差距和大,与c语言很相似.主要学的有: 1.引入js的三种方法:外联,内联,嵌套 2.标识符:第一个字符可以是任意Unicode字母,以及美元符号($)和下划线(_). - 第 ...

  6. java常用注释

    @see 加入超链接 @see 类名 @see 完整类名 @see 完整类名#方法名 @version 版本信息 @author 作者信息 @param 参数名 说明 @return 说明 @exce ...

  7. 关于TortoiseGit使用的心得

    花了我一个晚上,终于弄明白为什么总是 push 失败的原因了!竟然是因为我用的是注册的用户名而不是邮箱名……囧死. 另外搞清楚了一个问题,就是 Git 和远程仓库交互有两种方式,即 https 方式和 ...

  8. 使用Object类型的多态引用是会付出代价的

    import java.util.*; public class FiveShi { String name; public void eat(){ System.out.println(" ...

  9. C# 水印透明度图片

    /// <summary> /// 在一张图片的指定位置处加入一张具有水印效果的图片 /// </summary> /// <param name="Sourc ...

  10. web浏览器兼容简要整理

    ajax的创建 if (window.XMLHttpRequest) { var xhr = new XMLHttpRequest(); } else { //IE6及其以下版本浏览器 var xhr ...