包含min函数的栈
public class Solution {
Stack<Integer> stack1=new Stack<Integer>();
Stack<Integer> stack2=new Stack<Integer>();
public void push(int node) {
if(stack2.isEmpty()){
stack1.push(node);
stack2.push(node);
}else {
stack1.push(node);
if(node<=stack2.peek()){
stack2.push(node);
}
}
}
public void pop() {
int n=stack1.pop();
if(n==stack2.peek()){
stack2.pop();
}
}
public int top() {
return stack1.peek();
}
public int min() {
return stack2.peek();
}
}
查看24道真题和解析