题解 | #包含min函数的栈#
包含min函数的栈
https://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49
import java.util.*;
import java.util.Stack;
public class Solution {
private Stack<Integer> stack = new Stack<>();
public void push(int node) {
stack.push(node);
}
public void pop() {
stack.pop();
}
public int top() {
return stack.peek();
}
public int min() {
Stack<Integer> tmp = new Stack<>();
Integer pop = null;
while(!stack.isEmpty()) {
Integer cu = stack.pop();
if(pop == null) {
pop = cu;
}
if(pop > cu) {
pop = cu;
}
tmp.push(cu);
}
while(!tmp.isEmpty()) {
stack.push(tmp.pop());
}
return pop;
}
}
查看11道真题和解析