(LeetCode)两个队列来实现一个栈
原题例如以下:
Implement the following operations of a stack using queues.
- push(x) -- Push element x onto stack.
- pop() -- Removes the element on top of the stack.
- top() -- Get the top element.
- empty() -- Return whether the stack is empty.
Notes:
- You must use only standard operations of a queue -- which means only
push
,
to backpeek/pop from front
,size
,
andis empty
operations are valid. - Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
- You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
基本思路是:如果有两个队列Q1和Q2,当二者都为空时。入栈操作能够用入队操作来模拟,能够随便选一个空队列,如果选Q1进行入栈操作。如今如果a,b,c依次入栈了(即依次进入队列Q1)。这时如果想模拟出栈操作,则须要将c出栈。由于在栈顶。这时候能够考虑用空队列Q2,将a,b依次从Q1中出队,而后进入队列Q2,将Q1的最后一个元素c出队就可以。此时Q1变为了空队列。Q2中有两个元素,队头元素为a。队尾元素为b。接下来如果再运行入栈操作,则须要将元素进入到Q1和Q2中的非空队列,即进入Q2队列,出栈的话,就跟前面的一样。将Q2除最后一个元素外所有出队,并依次进入队列Q1,再将Q2的最后一个元素出队就可以。
Java实现代码例如以下:
class MyStack { LinkedList<Integer> queue1 = new LinkedList<Integer>();
LinkedList<Integer> queue2 = new LinkedList<Integer>();
// Push element x onto stack.
public void push(int x) {
if(queue1.size()==0&&queue2.size()==0)
{
queue1.offer(x);
}
else if(queue1.size()==0)
{
queue2.offer(x);
}
else
{
queue1.offer(x);
}
} // Removes the element on top of the stack.
public void pop() {
if(queue1.size()!= 0)
{
int length = queue1.size();
for(int i =0;i<length-1;i++)
{
queue2.offer(queue1.poll());
}
queue1.poll();
}
else
{
int length = queue2.size();
for(int i =0;i<length-1;i++)
{
queue1.offer(queue2.poll());
}
queue2.poll();
}
}
// Get the top element.
public int top() {
if(queue1.size()!= 0)
{
int length = queue1.size();
for(int i =0;i<length-1;i++)
{
queue2.offer(queue1.poll());
}
int result = queue1.element();
queue2.offer(queue1.poll());
return result;
}
else
{
int length = queue2.size();
for(int i =0;i<length-1;i++)
{
queue1.offer(queue2.poll());
}
int result = queue2.element();
queue1.offer(queue2.poll());
return result;
}
} // Return whether the stack is empty.
public boolean empty() {
return queue1.size()==0&&queue2.size()==0;
} }
(LeetCode)两个队列来实现一个栈的更多相关文章
- 《剑指Offer》附加题_用两个队列实现一个栈_C++版
在<剑指Offer>中,在栈和队列习题中,作者留下来一道题目供读者自己实现,即"用两个队列实现一个栈". 在计算机数据结构中,栈的特点是后进先出,即最后被压入(push ...
- 剑指offer编程题Java实现——面试题7相关题用两个队列实现一个栈
剑指offer面试题7相关题目:用两个队列实现一个栈 解题思路:根据栈的先入后出和队列的先入先出的特点1.在push的时候,把元素向非空的队列内添加2.在pop的时候,把不为空的队列中的size()- ...
- Leetcode 225 两个队列实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- C++两个队列实现一个栈
C++两个队列实现一个栈 /* * source.cpp * * Created on: 2015年6月21日 * Author: codekiller */ #include "iostr ...
- java两个栈实现一个队列&&两个队列实现一个栈
栈:先进后出 队列:先进先出 两个栈实现一个队列: 思路:先将数据存到第一个栈里,再将第一个栈里的元素全部出栈到第二个栈,第二个栈出栈,即可达到先进先出 源码: class Queue<E&g ...
- 两队列模拟一个栈,python实现
python实现两个队列模拟一个栈: class Queue(object): def __init__(self): self.stack1=[] self.stack2=[] def enqueu ...
- python两个队列实现一个栈和两个栈实现一个队列
1.两个栈实现一个队列 两个栈stack1和stack2, push的时候直接push进stack1,pop时需要判断stack1和stack2中的情况.如果stack2不为空的话,直接从stack2 ...
- 前、中、后序遍历随意两种是否能确定一个二叉树?理由? && 栈和队列的特点和区别
前序和后序不能确定二叉树理由:前序和后序在本质上都是将父节点与子结点进行分离,但并没有指明左子树和右子树的能力,因此得到这两个序列只能明确父子关系,而不能确定一个二叉树. 由二叉树的中序和前序遍历序列 ...
- [LeetCode] Binary Tree Level Order Traversal 与 Binary Tree Zigzag Level Order Traversal,两种按层次遍历树的方式,分别两个队列,两个栈实现
Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...
随机推荐
- AVC1与H264的差别
今天上网时偶尔发现这个在我脑海里疑惑的问题的答案. H.264 Video Types The following media subtypes are defined for H.264 video ...
- IE下判断IE版本语法使用
先摆一下判断IE版本语法 <!--[if lte IE 6]> <![endif]--> IE6及其以下版本可见 <!--[if lte IE 7]> <![ ...
- 好看的Select下拉框是如何制造的
现在在大多数网站中都能看到很华丽的Select下拉框,他们是如何实现的呢?使用默认select肯定是不好实现,我们可以使用div+js去模拟出来select的功能,并且又能很简单的去美化它. CSS代 ...
- linux服务器内存占用太高-释放内存
修改/proc/sys/vm/drop_caches,释放Slab占用的cache内存空间(参考drop_caches的官方文档): Writing to this will cause the ke ...
- LDAP索引及缓存优化
一.设置索引 索引将查找信息和 Directory Server 条目关联起来. Directory Server支持以下几种索引: 1出现索引 (pres) - 列出了具有特定属性的条目,与属性的值 ...
- jquery之checkbox
//checkbox 数据回显 var publishRange=rowData.publishRange.split(","); for(var i = 0;i < pub ...
- 迟到的tkinter---学校选课刷屏器
今儿上午选修了一门<结构分析与程序设计>,发现是用VB编写结构力学受力图示的一门课,VB高中学过,但是基本已经忘得差不多了,今儿老师让用VB编写了一个计算器,匆忙的百度了一下后就完成了觉得 ...
- (转)Jquery弹窗插件Lhgdialog的用法
Lhgdialog的用法 大家都知道用js可以实现,但是在使用js实现的弹窗时得考虑很东西:浏览器的兼容.页面的交互等等问题. 在这里简单介绍一下lhgdialog的用法. 参数有: Title:弹窗 ...
- DateTime对象反序列化的那些事
今天在Android的Json反序列化过程中,Date类型无法转化成自己想要的格式,鉴于之前在C#的反序列话中也遇到过这个问题,解决的同时,顺手做个总结,供自己及需要的人日后查阅. 将Da ...
- iOS项目名称、版本号与屏幕分辨率
iOS的版本号,一个叫做Version,一个叫做Build,这两个值都可以在Xcode 中选中target,点击“Summary”后看到. Version在plist文件中的key是“CFBundle ...