3.2

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());
	    }
}
//结果
7
2
2
2
2
2
1
2



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值