LeetCode232: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 top, peek/pop from top, size, and is empty operations 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).
使用栈来实现一个队列。这个题目之前在《剑指offer》上面见过,没有什么好说的。使用两个栈,一个栈用来保存插入的元素。另外一个栈用来运行pop或top操作,每当运行pop或top操作时检查另外一个栈是否为空,假设为空,将第一栈中的元素所有弹出并插入到第二个栈中。再将第二个栈中的元素弹出就可以。须要注意的是这道题的编程时有一个技巧,能够使用peek来实现pop。这样能够降低反复代码。编程时要能扩展思维,假设因为pop函数的声明再前面就陷入用pop来实现peek的功能的话就会感觉无从下手了。
runtime:0ms
class Queue {
public:
// Push element x to the back of queue.
void push(int x) {
pushStack.push(x);
}
// Removes the element from in front of queue.
void pop(void) {
peek();//这里能够使用peek进行两个栈之间元素的转移从而避免反复代码
popStack.pop();
}
// Get the front element.
int peek(void) {
if(popStack.empty())
{
while(!pushStack.empty())
{
popStack.push(pushStack.top());
pushStack.pop();
}
}
return popStack.top();
}
// Return whether the queue is empty.
bool empty(void) {
return pushStack.empty()&&popStack.empty();
}
private:
stack<int> pushStack;//数据被插入到这个栈中
stack<int> popStack;//数据从这个栈中弹出
};
LeetCode232:Implement Queue using Stacks的更多相关文章
- LeetCode232 Implement Queue using Stacks Java 题解
题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the bac ...
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
- 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues
232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
- 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 ...
- Lintcode: Implement Queue by Stacks 解题报告
Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...
- 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) 4
232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...
- [Swift]LeetCode232. 用栈实现队列 | Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
随机推荐
- PLS-00157: AUTHID only allowed on schema-level programs解决办法 包体的过程使用调用者权限方法
在包体里写了一个过程,test执行时报错,但是如果把该过程单独拿出来创建一个,就能顺利执行. 在没加上调用者权 authid current_user之前,报错如下 ORA-01031: insuf ...
- jquery远程引用地址大全
jquery官方的引用地址,如图: <script typet="text/javascript" src="http://code.jquery.com/jque ...
- JavaScript绘图类 (DIV绘图)
主要的图形算法抄自一个叫w_jsGraphics.js的类库,第一次看到那个库的时候,感觉那是十分神奇的存在.不过估计现在那个库早就已经找不到了. 这是很早之前的一个DIV绘图类,那时候VML+SVG ...
- UITextField小结
//初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, ...
- 关于如何防止PHP漏洞?
踏入编程圈一年不到,之前写的文章一直放在个人博客上,以后我写的或整理的好的教程一定到园子里分享,只是园子里PHPer好像不怎么活跃,希望同行多多交流.这是我之前整理的一篇PHP漏洞文章! 漏洞无非这么 ...
- tomcat修改默认端口
1.webserver: tomcat2.version: Apache Tomcat/7.0.293.operation: 修改默认端口 3.1 修改tomcat目录下的/conf/server ...
- Kmeans原理与实现
原理 http://www.cnblogs.com/jerrylead/archive/2011/04/06/2006910.html 实现 http://www.cnblogs.com/zjutzz ...
- 03 Java图形化界面设计——布局管理器之FlowLayout(流式布局)
前文讲解了JFrame.JPanel,其中已经涉及到了空布局的使用.Java 虽然可以以像素为单位对组件进行精确的定位,但是其在不同的系统中将会有一定的显示差异,使得显示效果不尽相同,为此java提供 ...
- c#上传大文件方法
客户端代码: /// <summary> /// 将本地文件上传到指定的服务器(HttpWebRequest方法) /// </summary> /// <param n ...
- IdentityServer4环境部署失败分析贴(一)
前言: 在部署Idv4站点和其客户端在外网时,发现了许多问题,折腾了许久,翻看了许多代码,写个MD记录一下. 1.受保护站点提示错误: Unable to obtain configuration f ...