题目描述
定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。
import java.util.Stack;
public class Solution {
private static Stack<Integer> stack = new Stack<>();
private static Stack<Integer> stack_Min = new Stack<>();
public void push(int node) {
stack.push(node);
try{
if(node < stack_Min.peek()){
stack_Min.push(node);
}else{
stack_Min.push(stack_Min.peek());
}
}catch(Exception e){
stack_Min.push(node);
}
}
public void pop() {
stack.pop();
stack_Min.pop();
}
public int top() {
return stack.peek();
}
public int min() {
return stack_Min.peek();
}
}