<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)232:用棧實現(xiàn)隊列

          共 3350字,需瀏覽 7分鐘

           ·

          2021-04-08 10:06

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

          今天和大家聊的問題叫做 用棧實現(xiàn)隊列,我們先來看題面:
          https://leetcode-cn.com/problems/implement-queue-using-stacks/

          Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty).

          請你僅使用兩個棧實現(xiàn)先入先出隊列。隊列應(yīng)當(dāng)支持一般隊列支持的所有操作(push、pop、peek、empty)

          示例


          輸入:
          ["MyQueue", "push", "push", "peek", "pop", "empty"]
          [[], [1], [2], [], [], []]
          輸出:
          [null, null, null, 1, 1, false]

          解釋:
          MyQueue myQueue = new MyQueue();
          myQueue.push(1); // queue is: [1]
          myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
          myQueue.peek(); // return 1
          myQueue.pop(); // return 1, queue is [2]
          myQueue.empty(); // return false


          解題

          https://www.cnblogs.com/parzulpan/p/10061620.html

          路: 由于隊列是先進先出的,而棧是先進后出的,所以要用2個棧來實現(xiàn)隊列的入隊出隊功能,隊列的入隊功能與棧的一樣,出隊時,先將第一個棧中的元素全部彈出,并倒入到第二個棧中,將第二個棧中棧頂元素彈出,并將stack2中剩下的元素倒回到stack1中,即實現(xiàn)一次出隊。


          class MyQueue {
          public:
              /** Initialize your data structure here. */
              
              stack<int> s1, s2;
              
              MyQueue() {
                  
              }
              
              /** Push element x to the back of queue. */
              void push(int x) {
                  while(!s2.empty()) {
                      s1.push(s2.top());
                      s2.pop();
                  }
                  s2.push(x);
                  while(!s1.empty()) {
                      s2.push(s1.top());
                      s1.pop();
                  }
              }
              
              /** Removes the element from in front of queue and returns that element. */
              int pop() {
                  int a = s2.top();
                  s2.pop();
                  return a;
              }
              
              /** Get the front element. */
              int peek() {
                  return s2.top();
              }
              
              /** Returns whether the queue is empty. */
              bool empty() {
                  return s2.empty();
              }
          };


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

          上期推文:

          LeetCode1-220題匯總,希望對你有點幫助!

          LeetCode刷題實戰(zhàn)221:最大正方形

          LeetCode刷題實戰(zhàn)222:完全二叉樹的節(jié)點個數(shù)

          LeetCode刷題實戰(zhàn)223:矩形面積

          LeetCode刷題實戰(zhàn)224:基本計算器

          LeetCode刷題實戰(zhàn)225:用隊列實現(xiàn)棧

          LeetCode刷題實戰(zhàn)226:翻轉(zhuǎn)二叉樹

          LeetCode刷題實戰(zhàn)227:基本計算器 II

          LeetCode刷題實戰(zhàn)228:匯總區(qū)間

          LeetCode刷題實戰(zhàn)229:求眾數(shù) II

          LeetCode刷題實戰(zhàn)230:二叉搜索樹中第K小的元素

          LeetCode刷題實戰(zhàn)231:2的冪


          瀏覽 18
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

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

          手機掃一掃分享

          分享
          舉報
          <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>
                  影音先锋每日av 影音先锋嫖娼视频 | 狠狠久久免费电影 | 性爱午夜视频 | 久久久人妻熟妇精品无码蜜桃 | 青榴最新入口 |