leetCode(37):Implement Queue using Stacks
Implement the following operations of a queue using stacks.
- push(x) -- Push element x to the back of queue.
- pop() -- Removes the element from in front of queue.
- peek() -- Get the front element.
- empty() -- Return whether the queue is empty.
Notes:
- You must use only standard operations of a stack -- which means only
push,
to toppeek/pop from top,size,
andis emptyoperations are valid. - Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
- You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
两个栈,一个作为压入栈,一个作为弹出栈。当弹出栈为空时,把压入栈中的数据依次弹出并压入到弹出栈中。
假设两者均为空,说明队列为空。
class Queue {
public:
// Push element x to the back of queue.
void push(int x) {
push_stack.push(x);
}
// Removes the element from in front of queue.
void pop(void) {
if (pop_stack.empty())
{
while (!push_stack.empty())
{
pop_stack.push(push_stack.top());
push_stack.pop();
}
if (!pop_stack.empty())
pop_stack.pop();
}
else
{
pop_stack.pop();
}
}
// Get the front element.
int peek(void) {
if (pop_stack.empty())
{
while (!push_stack.empty())
{
pop_stack.push(push_stack.top());
push_stack.pop();
}
if (!pop_stack.empty())
return pop_stack.top();
}
else
{
return pop_stack.top();
}
return 0;
}
// Return whether the queue is empty.
bool empty(void) {
if (pop_stack.empty() && push_stack.empty())
return true;
else
return false;
}
private:
stack<int> pop_stack;
stack<int> push_stack;
};
leetCode(37):Implement Queue using Stacks的更多相关文章
- Leetcode 232 Implement Queue using Stacks 和 231 Power of Two
1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...
- [LeetCode] 232. Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- (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 ...
- Java [Leetcode 232]Implement Queue using Stacks
题目描述: Implement the following operations of a queue using stacks. push(x) -- Push element x to the b ...
- LeetCode 232 Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- LeetCode(23)-Implement Queue using Stacks
题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the bac ...
- Java for LeetCode 232 Implement Queue using Stacks
Stack<Integer> stack=new Stack<Integer>(); public void push(int x) { stack.push(x); } // ...
- Leetcode 232 Implement Queue using Stacks STL
本题用两个栈实现队列,用栈的基本操作去实现队列的所有基本操作push(),pop(),peek()以及empty() sa作为输入栈,sb作为输出栈,将sa输入元素的反转过来放到sb中 push与sa ...
- LeetCode 232 Implement Queue using Stacks 两个栈实现队列
class MyQueue { public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x ...
随机推荐
- Email List
题目:给几个Email的list,输出全部list的交集(在全部list中都出现过的email). 思路:用set记录前i个list中都含有的email,当进行第i+1时,检查每个email是否在该s ...
- 【bzoj1485:】【 [HNOI2009]有趣的数列】模任意数的卡特兰数
(上不了p站我要死了,侵权度娘背锅) Description 我们称一个长度为2n的数列是有趣的,当且仅当该数列满足以下三个条件: (1)它是从1到2n共2n个整数的一个排列{ai}: (2)所有的奇 ...
- OpenResty域名could not be resolved及dnsmasq配置
在本地开发中使用自己配置的域名例如:wuyachao.com配置在/etc/hosts,ping wuyachao.com显示ip为127.0.0.1,在使用lua_resty_http时候,会报错 ...
- C# 事件和Unity3D
http://zijan.iteye.com/blog/871207 翻译自: http://www.everyday3d.com/blog/index.php/2010/10/04/c-events ...
- 使用React开发
阅读目录 React的组件生命周期 JSX 语法 父组件传向子组件 子组件传向父(爷)组件 getDefaultProps && getInitialState 获取真实的DOM节点 ...
- ylb:事务
ylbtech_sqlserver create database bank go use bank go create table users ( uid ,), uname ) not null, ...
- 【Hadoop】Hadoop HA 部署 详细过程(架构、机器规划、配置文件、部署步骤)
1.概念.架构 2.配置文件示例.部署步骤 hadoop2.0已经发布了稳定版本了,增加了很多特性,比如HDFS HA.YARN等.最新的hadoop-2.4.1又增加了YARN HA 注意:apac ...
- asp.net服务器数据源控件学习笔记
1.数据绑定控件的DataSource属性只能接受三种接口类型的数据 (IListSource,IEnumerable,IDataSource) 2.要手动在已经绑定数据的数据绑定控件上添加自定义的数 ...
- python langid实现语种识别
2017-04-26 语料数据入库时有个小需求,需要用一个字段存储语料的语种,偶然发现langid可以实现这一功能,再次感叹python的好用! #coding=utf-8 import langid ...
- 2017.3.31 spring mvc教程(六)转发、重定向、ajax请求
学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...