zoukankan      html  css  js  c++  java
  • 3.2---最小栈(CC150)

    //思路:入栈时不是最小值,永远都没机会成为最小值。

    import java.util.Stack; class MinStack { private Stack<Integer> stack = new Stack<Integer>(); private Stack<Integer> minStack = new Stack<Integer>(); public void push(int x) { stack.push(x); if(!minStack.empty()) { int min = minStack.peek(); if(x <= min) { minStack.push(x); } } else { minStack.push(x); } } public void pop() { if(minStack.size() != 0 && ((int)stack.peek() == (int)minStack.peek())) { minStack.pop(); } stack.pop(); } public int top() { return (int)stack.peek(); } public int getMin() { return (int)minStack.peek(); } }

      

  • 相关阅读:
    uva-321-暴力枚举-隐式图搜索
    uva-704-暴力枚举-双向bfs
    整数的无符号编码和有符号编码
    HDU 5793
    HDU 5730
    HDU 5740
    HDU 5768
    HDU 1194
    HDU 1086
    HDU 5145
  • 原文地址:https://www.cnblogs.com/yueyebigdata/p/5060723.html
Copyright © 2011-2022 走看看