設計乙個支援 push ,pop ,top 操作,並能在常數時間內檢索到最小元素的棧。
push(x) —— 將元素 x 推入棧中。
pop() —— 刪除棧頂的元素。
top() —— 獲取棧頂元素。
getmin() —— 檢索棧中的最小元素。
示例:輸入:
[「minstack」,「push」,「push」,「push」,「getmin」,「pop」,「top」,「getmin」]
[,[-2],[0],[-3],,,,]
輸出:[null,null,null,null,-3,null,0,-2]
解釋:minstack minstack = new minstack();
minstack.push(-2);
minstack.push(0);
minstack.push(-3);
minstack.getmin(); --> 返回 -3.
minstack.pop();
minstack.top(); --> 返回 0.
minstack.getmin(); --> 返回 -2.
pop、top 和 getmin 操作總是在 非空棧 上呼叫。
class
minstack
public
void
push
(int x)
public
void
pop(
)public
inttop()
public
intgetmin()
}/**
* 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.getmin();
*/
leetcode題 155 最小棧(簡單)
一 題目描述 155.最小棧 簡單 設計乙個支援 push,pop,top 操作,並能在常數時間內檢索到最小元素的棧。push x 將元素 x 推入棧中。pop 刪除棧頂的元素。top 獲取棧頂元素。getmin 檢索棧中的最小元素。示例 minstack minstack new minstack...
leetcode 棧 155 最小棧
設計乙個支援 push,pop,top 操作,並能在常數時間內檢索到最小元素的棧。push x 將元素 x 推入棧中。pop 刪除棧頂的元素。top 獲取棧頂元素。getmin 檢索棧中的最小元素。示例 minstack minstack new minstack minstack.push 2 m...
LeetCode第155題 最小棧
設計乙個支援 push,pop,top 操作,並能在常數時間內檢索到最小元素的棧。push x 將元素 x 推入棧中。pop 刪除棧頂的元素。top 獲取棧頂元素。getmin 檢索棧中的最小元素。示例 minstack minstack new minstack minstack.push 2 m...