feat: 2025-10-29打卡
This commit is contained in:
45
155.min-stack.java
Normal file
45
155.min-stack.java
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* @lc app=leetcode id=155 lang=java
|
||||||
|
*
|
||||||
|
* [155] Min Stack
|
||||||
|
*/
|
||||||
|
|
||||||
|
// @lc code=start
|
||||||
|
class MinStack {
|
||||||
|
private PriorityQueue<Integer> pq;
|
||||||
|
private Deque<Integer> deque;
|
||||||
|
|
||||||
|
public MinStack() {
|
||||||
|
pq = new PriorityQueue<>();
|
||||||
|
deque = new ArrayDeque<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void push(int val) {
|
||||||
|
pq.add(val);
|
||||||
|
deque.addLast(val);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void pop() {
|
||||||
|
int v = deque.removeLast();
|
||||||
|
pq.remove(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int top() {
|
||||||
|
return deque.peekLast();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getMin() {
|
||||||
|
return pq.peek();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Your MinStack object will be instantiated and called as such:
|
||||||
|
* MinStack obj = new MinStack();
|
||||||
|
* obj.push(val);
|
||||||
|
* obj.pop();
|
||||||
|
* int param_3 = obj.top();
|
||||||
|
* int param_4 = obj.getMin();
|
||||||
|
*/
|
||||||
|
// @lc code=end
|
||||||
|
|
||||||
Reference in New Issue
Block a user