常数时间内查找栈中最小的元素

import java.util.Stack;

/**
 * @Author JH
 * @CreateDate 18-6-9
 * @Description 栈中最小的元素
 */
public class MinStack {
    private Stack minStack=new Stack();
    private Stack element=new Stack();
    public E min(){
        if (!minStack.isEmpty())
        return minStack.peek();
        return null;
    }
    public void push(E e){
        element.push(e);
        if (!minStack.isEmpty()){
           E min=minStack.peek();
           if (e.compareTo(min)<0){
               minStack.push(e);
           }else{
               minStack.push(min);
           }
         }else{
            minStack.push(e);
        }
    }
    public E pop(){
        if (!element.isEmpty()){
            minStack.pop();
            return element.pop();
        }
        return null;
    }
    public  void print(){
        while (!element.isEmpty()){
            System.out.print(element.pop()+"  ");
        }
    }
    public static void main(String[] args) {
        MinStack s=new MinStack();
        s.push(3);
        System.out.println(s.min());
        s.push(4);
        System.out.println(s.min());
        s.push(2);
        System.out.println(s.min());
        s.push(3);
        System.out.println(s.min());
        s.pop();
        System.out.println(s.min());
        s.pop();
        System.out.println(s.min());
        s.pop();
        s.push(0);
        System.out.println(s.min());
        System.out.println(s.minStack.size());
    }
}

你可能感兴趣的:(算法,每日算法)