LeetCode上面的一道题目。原文例如以下:

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 topsize,
    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).

我的思路是创建两个栈S1和S2。入队时。将元素压入S1,出队时,假设栈S2中的元素个数为0,则将S1中的元素一个个压入S2,并弹出最后压入的那个元素。假设栈S2中的元素个数不为0,则直接弹出S2中的顶元素。

代码例如以下:

class MyQueue {
// Push element x to the back of queue.
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int x) {
stack1.push(x);
} // Removes the element from in front of queue.
public void pop() {
if(stack2.size()==0)
{
int m = stack1.size();
for(int i=0;i<m;i++)
{
stack2.push(stack1.pop());
}
}
stack2.pop();
} // Get the front element.
public int peek() {
if(stack2.size()==0)
{
int m = stack1.size();
for(int i=0;i<m;i++)
{
stack2.push(stack1.pop());
}
}
return stack2.peek();
} // Return whether the queue is empty.
public boolean empty() {
return stack1.size()==0&&stack2.size()==0;
}
}

(LeetCode)用两个栈实现一个队列的更多相关文章

  1. Python两个栈实现一个队列

    牛客网原题: 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型.   实现这个算法的方式有很多种,这里就写一种比较简单易懂的:虽然可能算法和效率上不太出色,当大多数人 ...

  2. web前端面试系列 - 数据结构(两个栈模拟一个队列)

    一. 用两个栈模拟一个队列 思路一: 1. 一个栈s1作为数据存储,另一个栈s2,作为临时数据存储. 2. 入队时将数据压人s1 3. 出队时将s1弹出,并压人s2,然后弹出s2中的顶部数据,最后再将 ...

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

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

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

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

  5. 两个栈实现一个队列,C语言实现,队列可伸缩,容纳任意数目的元素。

    一.思路:1.创建两个空栈A和B:2.A栈作为队列的入口,B栈作为队列的出口:3.入队列操作:即是入栈A:4.出队列操作:若栈B为空,则将A栈内容出栈并压人B栈,再出 B栈:不为空就直接出栈: 二.代 ...

  6. 剑指offer(五):用两个栈实现一个队列

    题目: 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 解决办法: 队列先进先出,栈先进后出(stack1和stack2) 其实主要要注意的点是: ①在添加时直接 ...

  7. 剑指offer:用两个栈实现一个队列

    题目 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 解题思路 用一个栈A来保存入栈,当要出栈的时候,将栈A的元素按照栈后进先出的特点转移到栈B中(此时栈A为空了 ...

  8. 用两个栈实现一个队列(C++)

    分析 栈:后进先出 队列:先进先出 要使用两个栈实现队列(先进先出),主要思路是 1.插入一个元素:直接将元素插入stack1即可. 2.删除一个元素:当stack2不为空时 ,直接弹出栈顶元素,当s ...

  9. 【校招面试 之 剑指offer】第9-1题 用两个栈实现一个队列

    #include<iostream> #include<stack> using namespace std; template <typename T> void ...

随机推荐

  1. ubuntu 上使用valgrind

    Valgrind是一个GPL的软件,用于Linux(For x86, amd64 and ppc32)程序的内存调试和代码剖析.你可以在它的环境中运行你的程序来监视内存的使用情况,比如C 语言中的ma ...

  2. 基于webview的选择滑动控件(PC和wap版)

    有了webview,大家开发ios或者安卓的app就方便很多啦. 第一可以增量更新: 第二webview可以同时兼容ios和安卓,减少开发量哦. --------------------------- ...

  3. Yahoo邮箱最后登录,成为历史!

  4. vsphere HA内幕变化

    see aslo:http://www.yellow-bricks.com/vmware-high-availability-deepdiv/ HA Deepdive My posts on VMwa ...

  5. MySql SqlServer Sqlite中关于索引的创建

    最近要更新Cocon90.Db库,令其ORM创建表时实现索引的添加.因此总结下列常用Sql,供大家学习与参考. 一.SqlServer中创建索引可以这样: ) Create Table Test ( ...

  6. 使用sphinx自动提取python中的注释成为接口文档

    写好了代码,交付给他人使用的时候,查看代码固然可以了解各类和函数的功能细节,但接口文档能更方便的查找和说明功能.所以,一价与代码同步的接口文档是很有必要的.sphinx可以根据python中的注释,自 ...

  7. linux下时间有关的函数和结构体

    1.时间类型.Linux下常用的时间类型有6个:time_t,struct timeb, struct timeval,struct timespec,clock_t, struct tm. (1) ...

  8. GitHub搭建个人网站详细教程

    GitHub搭建个人网站详细教程: http://blog.csdn.net/gane_cheng/article/details/52203759

  9. sqlserver 日志查看

    sqlserve的ErrorLog文件有时候会碰到文件很大的情况,可能通过命令xp_readerrorlog 或 sp_readerrorlog 执行,可以加搜索文本或起止时间 -- 日志查看 --e ...

  10. java 判断是否为纯数字

      java 判断是否为数字格式 CreateTime--2017年12月1日10:37:00 Author:Marydon java 判断是否为数字格式 /** * 判断是否为数字格式不限制位数 * ...