<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      DAY10 棧與隊列part01

       

      理論基礎

      文章講解:https://programmercarl.com/%E6%A0%88%E4%B8%8E%E9%98%9F%E5%88%97%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html

      232.用棧實現隊列

       注意為什么要用兩個棧

      題目鏈接/文章講解/視頻講解:https://programmercarl.com/0232.%E7%94%A8%E6%A0%88%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97.html

       1 class MyQueue {
       2 public:
       3     stack<int> stIn;
       4     stack<int> stOut;
       5     MyQueue() {
       6     }
       7     
       8     void push(int x) {
       9         stIn.push(x);
      10     }
      11     
      12     int pop() {
      13         int result;
      14         if(stOut.empty())
      15         {
      16             while(!stIn.empty())
      17             {
      18                 stOut.push(stIn.top());
      19                 stIn.pop();
      20             }
      21         }
      22         result=stOut.top();
      23         stOut.pop();
      24         return result;
      25     }
      26     
      27     int peek() {
      28         int result=this->pop();
      29         stOut.push(result);
      30         return result;
      31     }
      32     
      33     bool empty() {
      34         return (stIn.empty()&&stOut.empty());
      35     }
      36 };
      37 
      38 /**
      39  * Your MyQueue object will be instantiated and called as such:
      40  * MyQueue* obj = new MyQueue();
      41  * obj->push(x);
      42  * int param_2 = obj->pop();
      43  * int param_3 = obj->peek();
      44  * bool param_4 = obj->empty();
      45  */

       

      225. 用隊列實現棧

      題目鏈接/文章講解/視頻講解:https://programmercarl.com/0225.%E7%94%A8%E9%98%9F%E5%88%97%E5%AE%9E%E7%8E%B0%E6%A0%88.html

       

       1 class MyStack {
       2 public:
       3     queue<int> que;
       4 
       5     /** Initialize your data structure here. */
       6     MyStack() {
       7 
       8     }
       9 
      10     /** Push element x onto stack. */
      11     void push(int x) {
      12         que.push(x);
      13     }
      14 
      15     /** Removes the element on top of the stack and returns that element. */
      16     int pop() {
      17         int size = que.size();
      18         size--;
      19         while (size--) { // 將隊列頭部的元素(除了最后一個元素外) 重新添加到隊列尾部
      20             que.push(que.front());
      21             que.pop();
      22         }
      23         int result = que.front(); // 此時彈出的元素順序就是棧的順序了
      24         que.pop();
      25         return result;
      26     }
      27 
      28     /** Get the top element.
      29      ** Can not use back() direactly.
      30      */
      31     int top(){
      32         int size = que.size();
      33         size--;
      34         while (size--){
      35             // 將隊列頭部的元素(除了最后一個元素外) 重新添加到隊列尾部
      36             que.push(que.front());
      37             que.pop();
      38         }
      39         int result = que.front(); // 此時獲得的元素就是棧頂的元素了
      40         que.push(que.front());    // 將獲取完的元素也重新添加到隊列尾部,保證數據結構沒有變化
      41         que.pop();
      42         return result;
      43     }
      44 
      45     /** Returns whether the stack is empty. */
      46     bool empty() {
      47         return que.empty();
      48     }
      49 };

      20. 有效的括號

      題目鏈接/文章講解/視頻講解:https://programmercarl.com/0020.%E6%9C%89%E6%95%88%E7%9A%84%E6%8B%AC%E5%8F%B7.html

       三種不匹配的情況:

      左括號多——遍歷結束棧不為空

      右括號多——遍歷未結束棧即為空

      左右括號不匹配——右括號與棧頂部不匹配(技巧:遍歷左括號時壓棧元素為對應的右括號)

       1 class Solution {
       2 public:
       3     bool isValid(string s) {
       4         stack<int> sta;
       5         for(char c : s)
       6         {
       7             if(c=='(') sta.push(')');
       8             else if(c=='{') sta.push('}');
       9             else if(c=='[') sta.push(']');
      10             else 
      11             {
      12                 if(sta.empty()||c!=sta.top()) return false;
      13                 else sta.pop();
      14             }
      15         }
      16         if(!sta.empty()) return false;
      17         else return true;
      18     }
      19 };

      1047. 刪除字符串中的所有相鄰重復項

       用字符串模擬棧即可 注意pop_back() push_back() string.back();

      題目鏈接/文章講解/視頻講解:https://programmercarl.com/1047.%E5%88%A0%E9%99%A4%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E7%9A%84%E6%89%80%E6%9C%89%E7%9B%B8%E9%82%BB%E9%87%8D%E5%A4%8D%E9%A1%B9.html

       1 class Solution {
       2 public:
       3     string removeDuplicates(string s) {
       4         string res;
       5         for(char c : s)
       6         {
       7             if(res.empty()||c!=res.back()) res.push_back(c);
       8             else res.pop_back();
       9         }
      10         return res;
      11     }
      12 };

       

      posted @ 2024-07-26 20:24  xzdmzrc  閱讀(31)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 成a人片亚洲日本久久| 激情啪啪啪一区二区三区| 欧美成人午夜在线观看视频| 久久中文字幕无码专区| 亚洲精品国产综合麻豆久久99| 亚洲大尺度一区二区三区| 色综合中文综合网| 国产精品自在线拍国产手机版| 亚洲伊人精品久视频国产| 精品国偷自产在线视频99| 国产无遮挡又黄又爽又色| 国产成人精品永久免费视频| 久在线精品视频线观看| 99久久婷婷国产综合精品青草漫画| 国内精品久久黄色三级乱| 国产午夜精品福利视频| 午夜福利国产片在线视频| 国产初高中生粉嫩无套第一次| 威信县| 天天爽天天摸天天碰| 日本黄色一区二区三区四区| 久久精品国产亚洲av热一区 | 国产精品午夜福利合集| 黑人av无码一区| 亚洲男人AV天堂午夜在| 国产AV影片麻豆精品传媒| 欧美精欧美乱码一二三四区| 无遮无挡爽爽免费视频| 一亚洲一区二区中文字幕| 久久精品国产88精品久久| 欧美大bbbb流白水| 五月综合网亚洲乱妇久久| 熟女女同亚洲女同中文字幕| 99精品国产成人一区二区| 久久精品国产久精国产| 日韩av在线一卡二卡三卡| 国产精品亚洲精品日韩已满十八小| 亚洲国产午夜精品福利| 国产精品一国产精品亚洲| 精品无码国产自产拍在线观看蜜| 色99久久久久高潮综合影院|