国产探花免费观看_亚洲丰满少妇自慰呻吟_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(); */
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 象州县| 婺源县| 郁南县| 壤塘县| 肥东县| 田林县| 千阳县| 上杭县| 怀化市| 读书| 修文县| 栾城县| 调兵山市| 普格县| 涞源县| 青龙| 深州市| 湘西| 中宁县| 武定县| 龙里县| 屏山县| 郎溪县| 包头市| 富源县| 逊克县| 墨竹工卡县| 南城县| 正宁县| 阿拉善盟| 洪雅县| 大理市| 南华县| 丽江市| 南雄市| 六安市| 武隆县| 天水市| 布拖县| 伽师县| 托里县|