原题例如以下:

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 back
    peek/pop from frontsize,
    and is 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)两个队列来实现一个栈的更多相关文章

  1. 《剑指Offer》附加题_用两个队列实现一个栈_C++版

    在<剑指Offer>中,在栈和队列习题中,作者留下来一道题目供读者自己实现,即"用两个队列实现一个栈". 在计算机数据结构中,栈的特点是后进先出,即最后被压入(push ...

  2. 剑指offer编程题Java实现——面试题7相关题用两个队列实现一个栈

    剑指offer面试题7相关题目:用两个队列实现一个栈 解题思路:根据栈的先入后出和队列的先入先出的特点1.在push的时候,把元素向非空的队列内添加2.在pop的时候,把不为空的队列中的size()- ...

  3. Leetcode 225 两个队列实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  4. C++两个队列实现一个栈

    C++两个队列实现一个栈 /* * source.cpp * * Created on: 2015年6月21日 * Author: codekiller */ #include "iostr ...

  5. java两个栈实现一个队列&&两个队列实现一个栈

    栈:先进后出  队列:先进先出 两个栈实现一个队列: 思路:先将数据存到第一个栈里,再将第一个栈里的元素全部出栈到第二个栈,第二个栈出栈,即可达到先进先出 源码: class Queue<E&g ...

  6. 两队列模拟一个栈,python实现

    python实现两个队列模拟一个栈: class Queue(object): def __init__(self): self.stack1=[] self.stack2=[] def enqueu ...

  7. python两个队列实现一个栈和两个栈实现一个队列

    1.两个栈实现一个队列 两个栈stack1和stack2, push的时候直接push进stack1,pop时需要判断stack1和stack2中的情况.如果stack2不为空的话,直接从stack2 ...

  8. 前、中、后序遍历随意两种是否能确定一个二叉树?理由? && 栈和队列的特点和区别

    前序和后序不能确定二叉树理由:前序和后序在本质上都是将父节点与子结点进行分离,但并没有指明左子树和右子树的能力,因此得到这两个序列只能明确父子关系,而不能确定一个二叉树. 由二叉树的中序和前序遍历序列 ...

  9. [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 ...

随机推荐

  1. 【SSH三框架】Struts2第六章的基础:他们拦截函数的定义

    干web当然,需要做的事情时,项目管理登录身份验证及其他权利.假设我们必须使用相应的登陆,未经允许是不可能的. 因此,我们需要使用拦截器,拦截功能struts2它集成.当然,有可能在Spring正在使 ...

  2. EJB开发第一个无状态会话bean、开发EJBclient

    开发第一个无状态会话bean EJB中的三中bean: 会话Bean(Session Bean) 负责与client交互,是编写业务逻辑的地方.在会话bean中能够通过JDBC直接操作数据库.但大多数 ...

  3. gcc和g++编译c或者c++文件碰到的问题

    gcc和g++都是GNU(组织)的一个编译器.        误区一:gcc只能编译c代码,g++只能编译c++代码      两者都可以,但是请注意:      1.后缀为.c的,gcc把它当作是C ...

  4. MVC实现类似QQ的网页聊天功能-ajax(下)

    此篇文章主要是对MVC实现类似QQ的网页聊天功能(上)的部分代码的解释. 首先说一下显示框的滚动条置底的问题: 结构很简单一个大的div(高度一定.overflow:auto)包含着两个小的div第一 ...

  5. Web的工作机制

    简要的介绍一下Web的工作机制,以便对开发JavaWeb项目有个更好的理解. 一.Web的概念     1.1    何为Web:Web是万维网(World Wide Web)的简称.Web出现以前, ...

  6. mybatis的详解

    最新不知道脑子怎么想的,突然对mybatis特别感兴趣,之前在学校的时候学过两天,有了一个简单的认识,工作以后,项目中也有用到,趁着兴趣还在,抓紧整理一个文档,方便学习mybatis,同时,自己也在巩 ...

  7. 小学生之KTV播放原理

    第一步: 创建一个Song类 //歌曲名称 public  string SongName { get; set; } //歌曲路劲 public string SongPath { get; set ...

  8. Could not find the Visual SourceSafe Internet Web Service connection information

    Visual SourceSafe Internet---------------------------Could not find the Visual SourceSafe Internet W ...

  9. C++从多n个数中选取m个数的组合

    //start 是从哪个开始取, picked代表已经取了多少个数 //process和data是全局变量数组 //语言说明比较难,我举个例子吧 //从[ 1, 2, 3, 4 ]中选取 2 个数 / ...

  10. [Leetcode][001] Two Sum (Java)

    题目在这里: https://leetcode.com/problems/two-sum/ [标签]Array; Hash Table [个人分析] 这个题目,我感觉也可以算是空间换时间的例子.如果是 ...