C++實(shí)現(xiàn)LeetCode(155.最小棧)
[LeetCode] 155. Min Stack 最小棧
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
- push(x) -- Push element x onto stack.
- pop() -- Removes the element on top of the stack.
- top() -- Get the top element.
- getMin() -- Retrieve the minimum element in the stack.
Example:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --> Returns -3.
minStack.pop();
minStack.top(); --> Returns 0.
minStack.getMin(); --> Returns -2.
這道最小棧跟原來的棧相比就是多了一個(gè)功能,可以返回該棧的最小值。使用兩個(gè)棧來實(shí)現(xiàn),一個(gè)棧來按順序存儲(chǔ) push 進(jìn)來的數(shù)據(jù),另一個(gè)用來存出現(xiàn)過的最小值。代碼如下:
C++ 解法一:
class MinStack { public: MinStack() {} void push(int x) { s1.push(x); if (s2.empty() || x <= s2.top()) s2.push(x); } void pop() { if (s1.top() == s2.top()) s2.pop(); s1.pop(); } int top() { return s1.top(); } int getMin() { return s2.top(); } private: stack<int> s1, s2; };
Java 解法一:
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(); } }
需要注意的是上面的 Java 解法中的 pop() 中,為什么不能用注釋掉那兩行的寫法,博主之前也不太明白為啥不能對(duì)兩個(gè) stack 同時(shí)調(diào)用 peek() 函數(shù)來比較,如果是這種寫法,那么不管 s1 和 s2 對(duì)棧頂元素是否相等,永遠(yuǎn)返回 false。這是為什么呢,這就要看 Java 對(duì)于peek的定義了,對(duì)于 peek() 函數(shù)的返回值并不是 int 類型,而是一個(gè) Object 類型,這是一個(gè)基本的對(duì)象類型,如果直接用雙等號(hào) == 來比較的話,肯定不會(huì)返回 true,因?yàn)槭莾蓚€(gè)不同的對(duì)象,所以一定要先將一個(gè)轉(zhuǎn)為 int 型,然后再和另一個(gè)進(jìn)行比較,這樣才能得到想要的答案,這也是 Java 和 C++ 的一個(gè)重要的不同點(diǎn)吧。
那么下面再來看另一種解法,這種解法只用到了一個(gè)棧,還需要一個(gè)整型變量 min_val 來記錄當(dāng)前最小值,初始化為整型最大值,然后如果需要進(jìn)棧的數(shù)字小于等于當(dāng)前最小值 min_val,則將 min_val 壓入棧,并且將 min_val 更新為當(dāng)前數(shù)字。在出棧操作時(shí),先將棧頂元素移出棧,再判斷該元素是否和 min_val 相等,相等的話將 min_val 更新為新棧頂元素,再將新棧頂元素移出棧即可,參見代碼如下:
C++ 解法二:
class MinStack { public: MinStack() { min_val = INT_MAX; } void push(int x) { if (x <= min_val) { st.push(min_val); min_val = x; } st.push(x); } void pop() { int t = st.top(); st.pop(); if (t == min_val) { min_val = st.top(); st.pop(); } } int top() { return st.top(); } int getMin() { return min_val; } private: int min_val; stack<int> st; };
Java 解法二:
public class MinStack { private int min_val = Integer.MAX_VALUE; private Stack<Integer> s = new Stack<>(); public MinStack() {} public void push(int x) { if (x <= min_val) { s.push(min_val); min_val = x; } s.push(x); } public void pop() { if (s.pop() == min_val) min_val = s.pop(); } public int top() { return s.peek(); } public int getMin() { return min_val; } }
Github 同步地址:
https://github.com/grandyang/leetcode/issues/155
類似題目:
參考資料:
https://leetcode.com/problems/min-stack/
https://leetcode.com/problems/min-stack/discuss/49014/java-accepted-solution-using-one-stack
到此這篇關(guān)于C++實(shí)現(xiàn)LeetCode(155.最小棧)的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)最小棧內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C++實(shí)現(xiàn)LeetCode(161.一個(gè)編輯距離)
- C++實(shí)現(xiàn)LeetCode(160.求兩個(gè)鏈表的交點(diǎn))
- C++實(shí)現(xiàn)LeetCode(904.水果裝入果籃)
- C++實(shí)現(xiàn)LeetCode(159.最多有兩個(gè)不同字符的最長(zhǎng)子串)
- C++實(shí)現(xiàn)LeetCode(158.用Read4來讀取N個(gè)字符之二 - 多次調(diào)用)
- C++實(shí)現(xiàn)LeetCode(157.用Read4來讀取N個(gè)字符)
- C++實(shí)現(xiàn)LeetCode(156.二叉樹的上下顛倒)
- C++實(shí)現(xiàn)LeetCode(162.求數(shù)組的局部峰值)
相關(guān)文章
Qt利用QChart實(shí)現(xiàn)實(shí)時(shí)波形圖的繪制
這篇文章主要介紹了Qt如何利用QChart實(shí)現(xiàn)實(shí)時(shí)波形圖的繪制,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)有一定是參考價(jià)值,需要的可以參考一下2022-06-06VS2019添加引用出錯(cuò):對(duì)COM組件的調(diào)用返回了錯(cuò)誤HRESULT E_FAIL(未能完成操作未指定的錯(cuò)誤)
這篇文章主要介紹了VS2019添加引用出錯(cuò):對(duì)COM組件的調(diào)用返回了錯(cuò)誤HRESULT E_FAIL(未能完成操作。未指定的錯(cuò)誤),需要的朋友可以參考下2020-07-07c語言標(biāo)準(zhǔn)庫(kù)中字符轉(zhuǎn)換函數(shù)和數(shù)字轉(zhuǎn)換函數(shù)
這篇文章主要介紹了c標(biāo)準(zhǔn)庫(kù)中字符轉(zhuǎn)換函數(shù)和數(shù)字轉(zhuǎn)換函數(shù),需要的朋友可以參考下2014-04-04C語言實(shí)現(xiàn)一個(gè)簡(jiǎn)單的掃雷游戲
掃雷是電腦上很經(jīng)典的游戲,特意去網(wǎng)上玩了一會(huì),幾次調(diào)試之后,發(fā)現(xiàn)這個(gè)比三子棋要復(fù)雜一些,尤其是空白展開算法上和堵截玩家有的一拼,與實(shí)際游戲差別較大,不能使用光標(biāo),下面來詳解每一步分析2021-10-10