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

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

leetcode-225-Implement Stack using Queues

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

問題

題目:[leetcode-225]

思路

一個實現隊列,一個輔助隊列。需要注意的是,helper函數需要些兩個。處理對尾的情形。

做題的時候別著急,把題看清楚了。 這個題本省我很快可以做完,就是因為看錯了queue的接口。所以,別著急。定思路的時候,不在乎那一點時間。

代碼

class MyStack {public: /** Initialize your data structure here. */ MyStack() {} /** Push element x onto stack. */ void push(int x) { que1.push(x); } /** Removes the element on top of the stack and returns that element. */ int pop() { int t = top(); helper(que1, que2); helper1(que2, que1); return t; } /** Get the top element. */ int top() { return que1.back(); } /** Returns whether the stack is empty. */ bool empty() { return que1.empty(); }PRivate: void helper( queue<int>& q1, queue<int>& q2 ){ while(!q1.empty()){ int front = q1.front(); q1.pop(); if(!q1.empty()) q2.push(front); } } void helper1( queue<int>& q1, queue<int>& q2 ){ while(!q1.empty()){ int front = q1.front(); q1.pop(); q2.push(front); } }private: queue<int> que1; queue<int> que2;};/** * Your MyStack object will be instantiated and called as such: * MyStack obj = new MyStack(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.top(); * bool param_4 = obj.empty(); */
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 通州区| 南木林县| 横山县| 黄陵县| 婺源县| 耒阳市| 东源县| 通许县| 禄劝| 广丰县| 布拖县| 留坝县| 青海省| 延吉市| 景谷| 依兰县| 榕江县| 谷城县| 济阳县| 闽清县| 和静县| 郴州市| 阜平县| 桂平市| 准格尔旗| 洞头县| 贵州省| 中阳县| 石首市| 北碚区| 广西| 长沙市| 涪陵区| 莆田市| 巴里| 澜沧| 兴安县| 和顺县| 威远县| 深水埗区| 普兰县|