Description:

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 top, size, 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).

用栈来实现队列的功能。类似的还有用队列实现栈的功能。

思路:栈和队列对数据的处理是不同的。栈是先进后出(FILO)队列是先进先出(FIFO)。所以要用两个栈来维护一个队列。一个数据栈,一个暂存数据栈。数据栈用来存储数据,暂存数据栈用来把数据栈中的数据首尾交换,模拟队列的数据操作。

代码:

class MyQueue {
Stack<Integer> stack1;
Stack<Integer> stack2; public MyQueue() {
stack1 = new Stack();
stack2 = new Stack();
} // Push element x to the back of queue.
public void push(int x) {
stack1.push(x);
} // Removes the element from in front of queue.
public void pop() {
//把栈中的元素移到另一个栈中,首尾倒序。
while(!stack1.empty()) {
stack2.push(stack1.pop());
}
//移除堆首元素。
stack2.pop();
//还原数据队列。
while(!stack2.empty()) {
stack1.push(stack2.pop());
}
} // Get the front element.
public int peek() {
//把栈中的元素移到另一个栈中,首尾倒序。
while(!stack1.empty()) {
stack2.push(stack1.pop());
}
//获取堆首元素。
int front = stack2.peek();
//还原数据队列。
while(!stack2.empty()) {
stack1.push(stack2.pop());
}
return front;
} // Return whether the queue is empty.
public boolean empty() {
return stack1.empty();
}
}

LeetCode——Implement Queue using Stacks的更多相关文章

  1. [LeetCode] Implement Queue using Stacks 用栈来实现队列

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  2. Leetcode Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  3. LeetCode Implement Queue using Stacks (数据结构)

    题意: 用栈来实现队列. 思路: 一个栈是不够的,至少要两个. (1)插入.永远只插入到stack1中(插到栈顶). (2)弹出.如果stack2不为空,直接弹出stack2的栈顶,否则,将stack ...

  4. LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4

    232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...

  5. leetcode:Implement Stack using Queues 与 Implement Queue using Stacks

    一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...

  6. 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues

    232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...

  7. leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues

    155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...

  8. Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

    1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...

  9. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

随机推荐

  1. kubernetes daahboard权限限制

    dashboard在多人使用的时候经常遇到误操作的情况,为了对dashboard进行限制,对dashboard进行了权限控制, 这里主要限制只允许pod被删除.1:创建对应权限的ClusterRole ...

  2. Hibernate- 动态实例查询

    什么是动态实例查询: 就是将查询出的单一列的字段,重新封装成对象,如果不适用特殊方法,会返回Object对象数组. 01.搭建环境 02.动态实例查询 需要使用相应的构造方法: public Book ...

  3. mongodb查询之从多种分类中获取各分类最新一条记录

    mongodb查询之从多种分类中获取各分类最新一条记录 2017年04月06日 13:02:47 monkey_four 阅读数:6707更多 个人分类: MongoDBJavaScript   文章 ...

  4. SQLSERVER2008中创建数据库发生无法获得数据库'model'上的排他锁

    SQLSERVER2005中创建数据库发生无法获得数据库'model'上的排他锁是怎么回事? 创建数据库失败,提示无法获得数据库‘model’上的排他锁,如下图所示: 解决方法: 在查询分析器中运行如 ...

  5. sparkr基本操作1

    由于装的sparkr是1.4版本的,老版本的很多函数已经不再适用了. 在2台服务器的组成的集群中测试了一版数据,熟悉下这个api的基本操作.​ libpath <- .libPaths() li ...

  6. 关于Unity中UI中的Image节点以及它的Image组件

    一.图片的Inspector面板属性 Texture Type:一般是选择sprite(2D and UI) Sprite Mode:一般是选择Single Packing Tag:打包的标志值,最后 ...

  7. implicit declaration of function 'copy_from_user'

    内核中使用copy_from_user()和copy_to_user()函数,编译出现错误: implicit declaration of function 'copy_from_user' 需要添 ...

  8. (转)live555从RTSP服务器读取数据到使用接收到的数据流程分析

    本文在linux环境下编译live555工程,并用cgdb调试工具对live555工程中的testProgs目录下的openRTSP的执行过程进行了跟踪分析,直到将从socket端读取视频数据并保存为 ...

  9. linux -- Ubuntu报错“unable to locate package...”

    有时候在Ubuntu命令行中执行安装某个文件的时候,如:sudo apt-get install xinit ,报 “unable to locate package...” 错误,解决办法如下 1. ...

  10. wd mycloud nas新玩法

    最近家里的小米路由器坏了,主要是硬盘读取不出,小米之家也无能为力,本想继续在小米之家买个小米路由器,后来了解到了nas,于是想折腾下.我的nas型号是wd mycloud,3TB内置硬盘.功能:可以组 ...