国产探花免费观看_亚洲丰满少妇自慰呻吟_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(); */
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 云林县| 桂阳县| 收藏| 平泉县| 白朗县| 华宁县| 竹北市| 得荣县| 历史| 潞城市| 兴城市| 古交市| 南雄市| 武冈市| 永安市| 金溪县| 凉城县| 沿河| 弋阳县| 阜康市| 鸡西市| 玛多县| 灵宝市| 雷波县| 黑水县| 岳普湖县| 尉犁县| 南宫市| 阳朔县| 屯昌县| 高密市| 桦川县| 垣曲县| 和静县| 济源市| 屯门区| 张掖市| 右玉县| 通州市| 龙江县| 邢台市|