本题用两个栈实现队列,用栈的基本操作去实现队列的所有基本操作push(),pop(),peek()以及empty()

sa作为输入栈,sb作为输出栈,将sa输入元素的反转过来放到sb中

push与sa有关,而pop(),peek()与sb有关,即将sa输入元素出栈放到sb中(函数move).

为此,只有两个栈为空才能让队列为空

 class Queue {
public:
// Push element x to the back of queue.
stack<int> sa;
stack<int> sb;
void move(){
while(!sa.empty()){
sb.push(sa.top());
sa.pop();
}
}
void push(int x) {
sa.push(x);
} // Removes the element from in front of queue.
void pop(void) {
if(!sb.empty()) sb.pop();
else{
move();
sb.pop();
}
} // Get the front element.
int peek(void) {
if(!sb.empty()) return sb.top();
else{
move();
return sb.top();
} } // Return whether the queue is empty.
bool empty(void) {
return sa.empty() && sb.empty();
}
};

Leetcode 232 Implement Queue using Stacks STL的更多相关文章

  1. Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

    1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...

  2. [LeetCode] 232. Implement Queue using Stacks 用栈来实现队列

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  3. (easy)LeetCode 232.Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  4. Java [Leetcode 232]Implement Queue using Stacks

    题目描述: Implement the following operations of a queue using stacks. push(x) -- Push element x to the b ...

  5. LeetCode 232 Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  6. Java for LeetCode 232 Implement Queue using Stacks

    Stack<Integer> stack=new Stack<Integer>(); public void push(int x) { stack.push(x); } // ...

  7. LeetCode 232 Implement Queue using Stacks 两个栈实现队列

    class MyQueue { public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x ...

  8. leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues

    155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...

  9. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

随机推荐

  1. 本地缺Android SDK版本20,Unable to resolve target 'android-20'

    解决方案一 本地缺Android SDK版本20,Unable to resolve target 'android-20' 通过SDK Manager安装一个Android 20. 解决方案二: L ...

  2. mysql术语解释

    数据库(database): 数据表的集合: 数据表 (table):数据的矩阵: 列(column): 相同属性的数据的集合: 行(row): 一个对象的各种属性数据的集合: 冗余():一个字段在多 ...

  3. 利用iframe无刷新上传文件的坑

    页面里经常要用到文件上传的功能,而且要求页面不刷新,先说一下原理:页面里放一个file控件和submit按钮,外面用form表单包住,给form表单加上对应的属性值,action.method.ent ...

  4. 使用 BeanCopier 复制对象

    Cglib是一款比较底层的操作java字节码的框架. BeanCopier是一个工具类,可以用于Bean对象内容的复制. 复制Bean对象内容的方法有很多,比如自己手动get set ,或者使用Pro ...

  5. 2015年九月八日---js学习总结

    参考书:javaScript Dom 编程的艺术 知识小结:一:js简史:前称ECMAScript 是一种脚本语言通常只能通过浏览器进行解释和执行. Dom(文档对象模型):是一套对文档的内容进行抽象 ...

  6. c#开发Mongo笔记第三篇

    今天主要测试了一下查询功能了,当然了主要还是为了让查询可以和我们平时使用的实体对象关联起来,并且 那些BsonDocument和Collection我们操作起来不是太方便的 还是首先定义了一个用户类, ...

  7. Github两步认证

    获取密钥:ssh-keygen -t rsa  切换到公钥所在路径:cd .ssh 查看该路径下的所有文件:ls 查看公钥:cat id_rsa.pub 获取密钥之后,去https://github. ...

  8. VB6.0 获取Excel文件工作表Sheet的名称

    获取Excel文件工作表Sheet的名称 '产生Excel文档 Dim xlapp, xlbook As Object Dim sSheetName As String Set xlapp = Cre ...

  9. 内置函数 和 select练习3

    19.  查询选修"3-105"课程的成绩高于"109"号同学成绩的所有同学的记录. select * from score where cno='3-105' ...

  10. ubuntu 14.04加入五笔输入法

    快捷键"ctrl+AIt+T",弹出终端,输入以下指令 //先卸载IBUS输入法 killall ibus-daemon sudo apt- get purge ibus ibus ...