設(shè)計(jì)一個(gè)棧

題目

??設(shè)計(jì)一個(gè)支持 push,pop,top 操作,并能在常數(shù)時(shí)間內(nèi)檢索到最小元素的棧。
?????push(x) -- 將元素 x 推入棧中。
?????pop() -- 刪除棧頂?shù)脑亍?/strong>
?????top() -- 獲取棧頂元素。
?????getMin() -- 檢索棧中的最小元素。

示例

MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --> 返回 -3.
minStack.pop();
minStack.top(); --> 返回 0.
minStack.getMin(); --> 返回 -2.

思路

請(qǐng)參考設(shè)計(jì)一getMin的棧

代碼演示

package com.itz.stackAndQueue;

import java.util.Stack;

/**
 * 設(shè)計(jì)一個(gè)支持 push,pop,top 操作,并能在常數(shù)時(shí)間內(nèi)檢索到最小元素的棧。
 * <p>
 * push(x) -- 將元素 x 推入棧中。
 * pop() -- 刪除棧頂?shù)脑亍? * top() -- 獲取棧頂元素。
 * getMin() -- 檢索棧中的最小元素。
 * 示例:
 * <p>
 * MinStack minStack = new MinStack();
 * minStack.push(-2);
 * minStack.push(0);
 * minStack.push(-3);
 * minStack.getMin();   --> 返回 -3.
 * minStack.pop();
 * minStack.top();      --> 返回 0.
 * minStack.getMin();   --> 返回 -2.
 * <p>
 * 來源:力扣(LeetCode)
 * 鏈接:https://leetcode-cn.com/problems/min-stack
 * 著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。
 */

/**
 * 執(zhí)行用時(shí) :
 * 8 ms
 * , 在所有 java 提交中擊敗了
 * 100.00%
 * 的用戶
 * 內(nèi)存消耗 :
 * 40.2 MB
 * , 在所有 java 提交中擊敗了
 * 95.58%
 * 的用戶
 */
public class MinStack {

    private Stack<Integer> stackData;
    private Stack<Integer> stackMin;

    /**
     * initialize your data structure here.
     */
    public MinStack() {
        this.stackData = new Stack<>();
        this.stackMin = new Stack<>();
    }

    public void push(int x) {
        if (this.stackMin.isEmpty()) {
            this.stackMin.push(x);
        } else if (x <= this.getMin()) {
            this.stackMin.push(x);
        }
        this.stackData.push(x);
    }

    public void pop() {
        if (this.stackData.isEmpty()) {
            throw new RuntimeException("你沒有添加數(shù)據(jù)");
        }

        if (stackData.pop() == this.getMin()) {
            this.stackMin.pop();
        }
    }

    public int top() {

        return stackData.peek();
    }

    public int getMin() {
        if (this.stackMin.isEmpty()) {
            throw new RuntimeException("你沒有添加數(shù)據(jù)");
        }
        return this.stackMin.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.getMin();
 */

總結(jié):

本題有兩種方案設(shè)計(jì)一getMin的棧 兩種方案的時(shí)間復(fù)雜度和空間復(fù)雜度都分別是O(1)、O(n),只是方法一:在壓入的時(shí)候稍微省空間、但彈出的時(shí)候稍微費(fèi)時(shí)間,方案二 在壓入的時(shí)候稍微費(fèi)空間、但彈出的時(shí)候稍微省時(shí)間。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • java可以用數(shù)組和ArrayList兩種實(shí)現(xiàn)我們選擇了較為簡(jiǎn)單的ArrayList來實(shí)現(xiàn) javascriptj...
    Ching_Lee閱讀 170評(píng)論 0 0
  • 題目 難度:★☆☆☆☆類型:棧 設(shè)計(jì)一個(gè)支持 push,pop,top 操作,并能在常數(shù)時(shí)間內(nèi)檢索到最小元素的棧。...
    玖月晴閱讀 1,772評(píng)論 1 0
  • 設(shè)計(jì)一個(gè)支持 push,pop,top 操作,并能在常數(shù)時(shí)間內(nèi)檢索到最小元素的棧。push(x) -- 將元素 x...
    genggejianyi閱讀 249評(píng)論 0 0
  • 設(shè)計(jì)一個(gè)支持 push,pop,top 操作,并能在常數(shù)時(shí)間內(nèi)檢索到最小元素的棧。 push(x) -- 將元素 ...
    小白學(xué)編程閱讀 1,524評(píng)論 2 1
  • 在這篇文章里,我們來實(shí)現(xiàn)自定義的鏈?zhǔn)綏?。首先我們來看看鏈?zhǔn)綏5慕Y(jié)構(gòu)及操作定義。 鏈?zhǔn)綏=Y(jié)構(gòu)定義 首先,新建兩個(gè)文件...
    我叫卡卡算了閱讀 1,003評(píng)論 0 2

友情鏈接更多精彩內(nèi)容