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 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).
两个栈,一个作为压入栈,一个作为弹出栈。当弹出栈为空时,把压入栈中的数据依次弹出并压入到弹出栈中。
假设两者均为空,说明队列为空。
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 ...
随机推荐
- vue的路由设置小结
vue的路由设置小结 // 异步路由的编写示例.其中针对component字段进行懒加载及分块处理,提升首屏加载速度的同时,也可以手动控制让某些页面合并到一个单独的js文件中,而不是每个页面都是一个j ...
- ELK故障:elk在运行一段时间后,没有数据。
故障排查: 1. 查看kafka.logstash.elasticsearch进程是否运行正常,显示正常. 2. 使用logstash在前台运行,有日志输出 3. 查看kafka的topic的offs ...
- 解密Java内存溢出之持久代
垃圾回收是Java程序员了解最少的一部分.他们认为Java虚拟机接管了垃圾回收,因此没必要去担心内存的申请,分配等问题.但是随着应用越来越复杂,垃圾回收也越来越复杂,一旦垃圾回收变的复杂,应用的性能将 ...
- POJ3294 Life Forms(二分+后缀数组)
给n个字符串,求最长的多于n/2个字符串的公共子串. 依然是二分判定+height分组. 把这n个字符串连接,中间用不同字符隔开,跑后缀数组计算出height: 二分要求的子串长度,判断是否满足:he ...
- (转)stage 3d or unity 3d
这个是这样子的.stage3d如果不使用flascc的话,性能会卡在as上面.你没卡是因为你用的效果还不够高级.往深了走的高端应用就是卡as性能上.这不是你代码能优化了的.我们后来都改用flascc写 ...
- [读书笔记] CSS揭秘-背景与边框
半透明边框 默认情况下,背景会延伸到边框所在的区域下层.可以通过background-clip属性调整该默认行为. border: 10px solid rgba(0,0,0,.2) backgrou ...
- 新建一个兼容eclipse和myeclipse、IDEA都兼容的项目结构(maven)
以下观点为个人理解,没实践过,后续再实现. 思路: 1.eclipse和myeclipse.IDEA这些开发工具新建的各自的项目时,都有自己的一套思路,项目结构都有各自的特点. 2.如果是这些开发工具 ...
- 如何快速搜索SQL数据库数据和对象
原文 如何快速搜索SQL数据库数据和对象 Frequently, developers and DBAs need to search databases for objects or data. I ...
- What is the purpose of mock objects?
Since you say you are new to unit testing and asked for mock objects in "layman's terms", ...
- 利用jquery.form.js实现将form提交转为ajax方式提交的方法(带上传的表单提交)
提供一种方法就是利用jquery.form.js. (1)这个框架集合form提交.验证.上传的功能. 核心方法 -- ajaxForm() 和 ajaxSubmit() $('#myForm').a ...