๐ ๅๆ Offer 30. ๅ ๅซminๅฝๆฐ็ๆ
2022ๅนด10ๆ10ๆฅ
- algorithm
๐ ๅๆ Offer 30. ๅ ๅซminๅฝๆฐ็ๆ
้พๅบฆ: ๐
้ฎ้ขๆ่ฟฐ
่งฃๆณ
class MinStack {
// ๆ่ทฏ๏ผ
// ๅๅฉ ่พ
ๅฉๆ ๏ผๅญๆพๆๅฐๅผ
LinkedList<Integer> stack;
LinkedList<Integer> ts;
/** initialize your data structure here. */
public MinStack() {
stack = new LinkedList<>();
ts = new LinkedList<>();
}
public void push(int x) {
stack.push(x);
if(ts.isEmpty() || x <= ts.peek()) {
ts.push(x);
}
}
public void pop() {
int val = stack.pop();
if(!ts.isEmpty() && val == ts.peek()) {
ts.pop();
}
}
public int top() {
return stack.peek();
}
public int min() {
return ts.peek();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.min();
*/