請在該類型中實現一個可以獲得棧中所含最小元素的min函數

import java.util.Stack;java

public class Solution {node

Stack<Integer> stack1=new Stack<Integer>();code

Stack<Integer> stack2=new Stack<Integer>();io

public void push(int node) {
     
      stack1.push(node);
    
    if(stack2.isEmpty())
        stack2.push(node);
    else
        if(stack2.peek()>=node)
            stack2.push(node);
    
    
}

public void pop() {
    if(stack1.peek()==stack2.peek())
         stack2.pop();
        stack1.pop();
       
}

public int top() {
    return stack1.peek();
}

public int min() {
    return stack2.peek();
}

}class

相關文章
相關標籤/搜索