3.2

来源:互联网 发布:韦小宝 知乎 编辑:程序博客网 时间:2024/04/28 13:20

Topic: How would you design a stack which, in addition to push and pop, also has a function min which returns the minimum element? Push, pop and min should all operate in O(1) time.

// 方法1:每一个节点都变成两个,自己的值及除它以外最小的。只要看栈顶元素,就可以知道所有元素的最小.Push时,给元素当前最小. (Each node has its own value and the minimum beneath itself. To find the min, just look at what the top element thinks is the min. When push an element onto the stack, given it the current minimum. )

// 方法2:优化。可能把同一个最小值存了很多遍。另开一个栈存最小值,该栈存的都是每一个节点(含它)以下的最小值。(If the stack is large, we waste a lot of space by keep track of the min for every single element. Use an additional stack which keeps track of the mins. )(见代码)

public class NodeWithMin {public int value;public int min;public NodeWithMin(int value, int min){this.value=value;this.min=min;}}
import java.util.Stack;public class StackWithMin{    private Stack<NodeWithMin> stack;    public StackWithMin(){    stack=new Stack<NodeWithMin>();    }        public void push(int value){        int newMin=Math.min(value, min());        stack.push(new NodeWithMin(value,newMin));        }                public void pop(){        stack.pop();        }                    public int min(){        if(stack.isEmpty()){        return Integer.MAX_VALUE;        }else{        return stack.peek().min;        }        }    public static void main(String args[]){     StackWithMin stack = new StackWithMin();        stack.push(7);        System.out.println(stack.min());        stack.push(2);        System.out.println(stack.min());        stack.push(3);        System.out.println(stack.min());        stack.push(4);        System.out.println(stack.min());        stack.push(2);        System.out.println(stack.min());        stack.pop();        System.out.println(stack.min());        stack.push(1);        System.out.println(stack.min());        stack.pop();        System.out.println(stack.min());    }}
import java.util.EmptyStackException;import java.util.Stack;public class StackWithMin { public StackWithMin(){        s1 = new Stack<Integer>();        s2 = new Stack<Integer>();    }        public void push(int data){        if(s2.empty())            s2.push(data);        else if(data <= s2.peek()){            s2.push(data);        }                s1.push(data);    }        public int pop(){        if(s1.empty())            throw new EmptyStackException();//用异常处理的办法来解决栈空的问题        int result = s1.pop();                if(min() == result)            s2.pop();                return result;    }        public int min(){        if(s2.empty())            throw new EmptyStackException();        return s2.peek();    }     private Stack<Integer> s1, s2;        public static void main(String args[]){        StackWithMin stack = new StackWithMin();        stack.push(7);        System.out.println(stack.min());        stack.push(2);        System.out.println(stack.min());        stack.push(3);        System.out.println(stack.min());        stack.push(4);        System.out.println(stack.min());        stack.push(2);        System.out.println(stack.min());        stack.pop();        System.out.println(stack.min());        stack.push(1);        System.out.println(stack.min());        stack.pop();        System.out.println(stack.min());    }}
//结果72222212



 

原创粉丝点击