本题用两个栈实现队列,用栈的基本操作去实现队列的所有基本操作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. iOS 设置 文字和 图片的位置

    1.我最开始实现这个采用的方法:重新自定义一个view,然后有两个属性label和imageView,然后设置位置布局,再添加单击手势,用代理回传点击方法. 2.第二种方法:自定义一个Button继承 ...

  2. spilt()的用法

    split() 方法用于把一个字符串分割成字符串数组.<script type="text/javascript"> var str="How are you ...

  3. flask--虚拟环境

    1.安装虚拟环境mosson@mosson:~$ sudo apt-get install virtualenv2.创建一个项目目录mosson@mosson:~$ mkdir myproject3. ...

  4. mysql 循环插入日期递增

    create procedure wk() begin declare i int; ; do insert into t (myday) values (date_sub(curdate(),int ...

  5. Servlet实现重定向的两种方式

    使用Servlet实现请求重定向:两种方式 1. response.setStatus(302); response.setHeader("location", "/Re ...

  6. sublime text保存时删除行尾空格

    打开sublime text,点击在Preferences, Settings-User打开的用户配置中加入以下一行: "trim_trailing_white_space_on_save& ...

  7. redis pipeline

    redis pipeline 简而言之就是把多个redis命令打包,一起发送给redis server,并且一起返回结果,减少客户端和服务器之间的多次“折返跑”

  8. iOS字符串为空的判断

    //判断字符串 -(BOOL) isValidString:(id)input { if (!input) { return NO; } if ((NSNull *)input == [NSNull ...

  9. python 高阶函数与装饰器

    高阶函数定义1.函数接收的参数是一个函数名2.函数的返回值是一个函数名以上两者满足任意一个,就是高阶函数装饰器定义本质就是函数,功能是为其他函数添加新功能 装饰器的原则 1.不修改被装饰函数的源代码( ...

  10. 必填项(required)

    当你设计表单时,你可以指定某些选项为必填项(required),只有当用户填写了该选项后,用户才能够提交表单. 例如,如果你想把一个文本输入字段设置为必填项,在你的input元素中加上required ...