Advertisement
tanglinghui

ctciCh3Q2

Jun 6th, 2012
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.47 KB | None | 0 0
  1. import java.util.Stack;
  2.  
  3. public class StackWithMin extends Stack<Integer> {
  4.  
  5.     Stack<Integer> s_min = new Stack<Integer>();
  6.    
  7.     public void push(int data) {
  8.         if (data <= this.min()) {
  9.             s_min.push(data);
  10.         }
  11.         super.push(data);
  12.     }
  13.  
  14.     public Integer pop() {
  15.         int value =  super.pop();
  16.         if (value == min()) {
  17.             s_min.pop();
  18.         }
  19.         return value;
  20.     }
  21.  
  22.     public int min() {
  23.         if(s_min.isEmpty()){
  24.             return Integer.MAX_VALUE;
  25.         }
  26.         return  s_min.peek();
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement