<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實戰(zhàn)155:最小棧

          共 2238字,需瀏覽 5分鐘

           ·

          2021-01-15 13:58

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做?最小棧,我們先來看題面:
          https://leetcode-cn.com/problems/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.

          題意


          設(shè)計一個支持 push ,pop ,top 操作,并能在常數(shù)時間內(nèi)檢索到最小元素的棧。

          • push(x) —— 將元素 x 推入棧中。

          • pop() —— 刪除棧頂?shù)脑亍?/span>

          • 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.


          解題

          思路:用一個int型成員變量min記錄當(dāng)前棧中的最小值


          public?class?MinStack?{
          ????private?LinkedList stack;
          ????List array;
          ????int?min;
          ????public?MinStack()?{
          ????????stack?= new?LinkedList<>();
          ????????array?= new?ArrayList<>();
          ????????min = Integer.MAX_VALUE;
          ????}
          ????public?void?push(int?x)?{
          ????????stack.push(x);
          ????????array.add(x);
          ????????min = Math.min(min, x);
          ????}
          ????public?void?pop()?{
          ????????int?num = stack.pop();
          ????????array.remove(array.size() - 1);
          ????????if(array.size() > 0) {
          ????????????min = array.get(0);
          ????????????for?(int?i = 0; i < array.size(); i++) {
          ????????????????if(min > array.get(i)) {
          ????????????????????min = array.get(i);
          ????????????????}
          ????????????}
          ????????}else?{
          ????????????min = Integer.MAX_VALUE;
          ????????}
          ????}
          ????public?int?top()?{
          ????????return?stack.peek();
          ????}
          ????public?int?getMin()?{
          ????????return?min;
          ????}
          }


          好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力。

          上期推文:

          LeetCode1-140題匯總,希望對你有點幫助!
          LeetCode刷題實戰(zhàn)141:環(huán)形鏈表
          LeetCode刷題實戰(zhàn)142:環(huán)形鏈表 II
          LeetCode刷題實戰(zhàn)143:重排鏈表
          LeetCode刷題實戰(zhàn)144:二叉樹的前序遍歷
          LeetCode刷題實戰(zhàn)145:二叉樹的后序遍歷
          LeetCode刷題實戰(zhàn)146:LRU 緩存機制
          LeetCode刷題實戰(zhàn)147:對鏈表進行插入排序
          LeetCode刷題實戰(zhàn)148:排序鏈表
          LeetCode刷題實戰(zhàn)149:直線上最多的點數(shù)
          LeetCode刷題實戰(zhàn)150:逆波蘭表達式求值
          LeetCode刷題實戰(zhàn)151:翻轉(zhuǎn)字符串里的單詞
          LeetCode刷題實戰(zhàn)152:乘積最大子數(shù)組
          LeetCode刷題實戰(zhàn)153:尋找旋轉(zhuǎn)排序數(shù)組中的最小值
          LeetCode刷題實戰(zhàn)154:尋找旋轉(zhuǎn)排序數(shù)組中的最小值 II


          瀏覽 61
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  成人禁区 | 国产大黄片久久久久久 | 伊人久久伊人 | 国产 亚洲 无码 激情 | 美女在线抠逼 |