乐趣区

包含Min函数的stack

题目
题解
import java.util.Stack;

public class Solution {

Stack<Integer> stack = new Stack();
Stack<Integer> helpStack = new Stack();
public void push(int node) {
int helpStackTop = node;
if (!helpStack.isEmpty()) {
helpStackTop = Math.min(helpStack.peek(), helpStackTop);
}
stack.push(node);
helpStack.push(helpStackTop);
}

public void pop() {
if (stack.isEmpty()) {
return;
}
stack.pop();
helpStack.pop();
}

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

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

退出移动版