LeetCode || Implement Stack using Queues
2015-07-12 11:35
381 查看
class Stack { public: // Push element x onto stack. void push(int x) { q1.push(x); } // Removes the element on top of the stack. void pop() { if(q1.size() == 0) { while(q2.size() > 1) { q1.push(q2.front()); q2.pop(); } q2.pop(); } else { while(q1.size() > 1) { q2.push(q1.front()); q1.pop(); } q1.pop(); } } // Get the top element. int top() { if(q1.size() == 0) { while(q2.size() > 1) { q1.push(q2.front()); q2.pop(); } int top = q2.front(); q1.push(top); q2.pop(); return top; } else { while(q1.size() > 1) { q2.push(q1.front()); q1.pop(); } int top = q1.front(); q2.push(top); q1.pop(); return top; } } // Return whether the stack is empty. bool empty() { if(q1.size() == 0 && q2.size() == 0) return true; return false; } queue<int> q1; queue<int> q2; };
相关文章推荐
- 函数参数之 传常引用(passed by reference to const)替换 传值(passed by value)
- 【LeetCode】225. Implement Stack using Queues
- 实时显示iOS编写UI代码效果
- LeetCode || Implement Queue using Stacks
- .net使用FluentValidation进行服务端验证。
- iOS UIImageView 圆角
- require.js的用法
- 2015年7月11日UITableView3
- openstack-glance-api.service start request repeated too quickly, refusing to start
- 关于UI的开发是使用代码还是Xib
- Optimization Techniques
- Optimization Techniques
- Fuel 5.1安装openstack I版本号环境
- css3新增加UI学习
- Implement Queue using Stacks
- qibuild的环境配置
- 详解 UIView 的 Tint Color 属性
- Win10首个候选版Build 10176上手:水印终于没了
- 安卓handler机制和RunOnUi方法都能修改主线程,两者的区别
- 教你如何调用百度编辑器ueditor的上传图片、上传文件等模块