剑指offer——(22)包含min函数的栈

版权声明:本文自由转载,转载请注明出处。 https://blog.csdn.net/qq_38071429/article/details/84993029

import java.util.Stack;
import java.util.ArrayList;
public class Solution {
    int min = 0,top = 0;
    boolean boo = false;
    Stack<Integer> stackMin = new Stack<Integer>();
    ArrayList<Integer> AL = new ArrayList<Integer>();
    public void push(int node) {
        stackMin.push(node);
        if(boo == false){
            min = node;
            boo = true;
            AL.add(min);  
        }
        if(node < min){
            int temp = min;
            min = node;
            node = temp;           
        }        
        AL.add(min);        
    }
    
    public void pop() {   
        if(!stackMin.empty()) 
            stackMin.pop();
        AL.remove(AL.size()-1);
    }
    
    public int top() {   
        //if(!stackMin.empty()) 
        return AL.get(AL.size()-1);
    }
    
    public int min() {
        //min = top();
        return top();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_38071429/article/details/84993029