国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

leetcode-232-Implement Queue using Stacks

2019-11-08 02:33:29
字體:
來源:轉載
供稿:網友

問題

題目:[leetcode-232]

思路

借助一個輔助棧去實現。把一個棧導入另一個棧,輔助的操作可以寫成一個函數。

代碼

class MyQueue {public: /** Initialize your data structure here. */ MyQueue() {} /** Push element x to the back of queue. */ void push(int x) { stk1.push(x); } /** Removes the element from in front of queue and returns that element. */ int pop() { helper(stk1, stk2); int front = stk2.top(); stk2.pop(); helper(stk2, stk1); return front; } /** Get the front element. */ int peek() { helper(stk1, stk2); int front = stk2.top(); helper(stk2, stk1); return front; } /** Returns whether the queue is empty. */ bool empty() { return stk1.empty(); }PRivate: void helper(stack<int>& s1, stack<int>& s2){ while(!s1.empty()){ int top = s1.top(); s1.pop(); s2.push(top); } }private: stack<int> stk1; stack<int> stk2;};/** * Your MyQueue object will be instantiated and called as such: * MyQueue obj = new MyQueue(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.peek(); * bool param_4 = obj.empty(); */
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 获嘉县| 上林县| 温州市| 绥棱县| 长春市| 清水河县| 汉阴县| 通江县| 丹巴县| 新津县| 潼关县| 普兰店市| 博客| 习水县| 兴安盟| 扶沟县| 临漳县| 田东县| 嘉义县| 招远市| 财经| 静海县| 施秉县| 专栏| 汉源县| 济阳县| 郓城县| 什邡市| 泰来县| 正镶白旗| 克什克腾旗| 綦江县| 商洛市| 新巴尔虎左旗| 平舆县| 呼图壁县| 资阳市| 株洲县| 华蓥市| 怀集县| 六枝特区|