包含Min函数的stack

6次阅读

共计 431 个字符,预计需要花费 2 分钟才能阅读完成。

题目
题解
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();
}
}

正文完
 0