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 ...
随机推荐
- 洛谷 P4551 最长异或路径
题目描述 给定一棵 nn 个点的带权树,结点下标从 11 开始到 NN .寻找树中找两个结点,求最长的异或路径. 异或路径指的是指两个结点之间唯一路径上的所有节点权值的异或. 输入输出格式 输入格式: ...
- JsonConfig处理日期时间
写在前面: 页面发送ajax请求到后台,后台返回对应的json格式数据给前台页面进行数据展示,如果json数据中含有日期时间,就需要对日期进行处理 下面是相关的代码部分 JsonConfig json ...
- About Markdown -- 进入Markdown园子
起初也就是打算简单一些Markdown在编辑Blog方面的一些常用操作和注意事项,没想到,一下没刹住,毫无防备地闯进了这个好趣的园子-. 1. 认识 Markdown HTML(HyperText M ...
- MongoDB 聚合Group(一)
原文:http://blog.csdn.net/congcong68/article/details/45012717 一.简介 db.collection.group()使用JavaScript,它 ...
- VUE -- stylus入门使用方法
sizes() 15px 10px sizes()[0] // => 15px stylus介绍 是个什么鬼?对于开发来说,CSS的弱点在于静态化.我们需要一个真正能提高开发效率的工具,LESS ...
- 设计模式之桥接模式(php实现)
github地址:https://github.com/ZQCard/design_pattern /** * 桥接模式 * 优点: * 1.分离抽象接口及其实现部分.提高了比继承更好的解决方案. * ...
- git push后自动部署
前提,服务器已经装好ssh,本地也已经将ssh 公钥传到服务器对应位置 先用 pbcopy < ~/.ssh/PRIVATE_KEY.pub 将公钥复制到剪贴板:通过 ssh USER@SERV ...
- 关于国内外CV领域牛人的博客链接 .
此文为转载文章,尊重知识产权http://blog.csdn.net/carson2005/article/details/6601109此为原文链接,感谢作者! 以下链接是关于计算机视觉(Compu ...
- ES6里关于函数的拓展(一)
一.形参默认值 Javascript函数有一个特别的地方,无论在函数定义中声明了多少形参,都可以传入任意数量的参数,也可以在定义函数时添加针对参数数量的处理逻辑,当已定义的形参无对应的传入参数时为其指 ...
- thrift.transport.TTransport.TTransportException: Could not start SASL: Error in sasl_client_start (-4) SASL(-4): no mechanism available: No worthy mechs found
thrift.transport.TTransport.TTransportException: Could not start SASL: Error in sasl_client_start (- ...