LeetCode 0155 Min Stack
作者:互联网
1. 题目描述
2. Solution 1
1、思路分析
用两个栈来实现,s1是数据输入栈,s2是s1中对应位置元素的最小值。
2、代码实现
package Q0199.Q0155MinStack;
import java.util.Stack;
public class MinStack {
private Stack<Integer> s1 = new Stack<>();
private Stack<Integer> s2 = new Stack<>();
public MinStack() {
}
public void push(int x) {
s1.push(x);
if (s2.isEmpty() || s2.peek() >= x) s2.push(x);
}
public void pop() {
int x = s1.pop();
if (s2.peek() == x) s2.pop();
}
public int top() {
return s1.peek();
}
public int getMin() {
return s2.peek();
}
}
标签:peek,int,s2,s1,public,0155,LeetCode,Stack 来源: https://www.cnblogs.com/junstat/p/16310565.html