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 ...
随机推荐
- XML签名
英文:https://www.javacodegeeks.com/2013/10/xml-security-with-digital-signature-in-java.html 中文:http:// ...
- jenkins笔记:手动更新插件
jenkins服务器连不了外网,需要手动更新插件.上https://wiki.jenkins-ci.org搜索相应的插件,下载Latest Release-最新版本的.hpi.在系统管理->插件 ...
- 分享Kali Linux 2017年第23周镜像文件
分享Kali Linux 2017年第23周镜像文件 Kali Linux官方于6月4日发布2017年的第23周镜像.这次维持了11个镜像文件的规模.默认的Gnome桌面的4个镜像,E17.KDE. ...
- MySQL注入工具sqlsus
MySQL注入工具sqlsus sqlsus是使用Perl语言编写的MySQL注入和接管工具.它可以获取数据库结构,实施注入查询,下载服务器的文件,爬取可写目录并写入后门,以及复制数据库文件等功能 ...
- ReactiveCocoa(一)
前言 之前总听别人说什么Reactive Cocoa + MVVM,但是没有找到讲解Reactive Cocoa相关的资料.结果进入新公司,项目里面有部分代码使用到了Reactive Cocoa,所以 ...
- java的一些基本常识
1.什么是java虚拟机?为什么把java称作是“无关平台的语言”? java虚拟机是一个可以执行Java字节码的虚拟进程.Java源文件被编译成能被Java虚拟机执行的字节码文件. Java 被设计 ...
- CocoaPods 错误 target overrides the `OTHER_LDFLAGS`...
Xcode 升级到 6.0 后,更新 CocoaPods,出现了如下的警告 [!] The `Paopao [Debug]` target overrides the `PODS_ROOT` buil ...
- fuser 和 lsof
FUSER fuser功能fuser 可以显示出当前哪个程序在使用磁盘上的某个文件.挂载点.甚至网络端口,并给出程序进程的详细信息. fuser显示使用指定文件或者文件系统的进程ID.默认情况下每个文 ...
- java 后台将base64字符串保存为图片
直接上代码: import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; impo ...
- hdu5884 Sort
//--------------------------------------------------------------- /*---贪心策略+二分+队列 -----将原数组排序,然后每次取k ...