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. 用STS创建Maven的Web项目<转>

    右键New——>other——>Maven——>Maven Project 弹出框中点击Next,在Filter中写上:webapp. 然后在下面的框中选择org.apache.ma ...

  2. Missing artifact jdk.tools:jdk.tools:jar:1.8 pom.xml

    在maven项目中出现下面错误信息: Description Resource Path Location Type Missing artifact jdk.tools:jdk.tools:jar: ...

  3. 设置cookies第二天0点过期

    网上这个资料很少,试了csdn其中一个人的方法去算时间,返回的是Invalid Date,不知道是不是不同IE版本 返回结果不同 后面找到另外一个很简答粗暴的方法 直接加一天后,设置时分秒为0,代码如 ...

  4. 公司内网成功实现WSUS在不连外网的条件下更新补丁包!

    微软的WSUS的命令行很有帮助! 为了便于管理,WSUS服务器中提供了一个命令行工具WSUSUtil.exe,你可以使用它完成一些在WSUS管理控制台中不能进行的任务,例如导入导出数据等等.WSUSU ...

  5. Workerman

    What is it Workerman is a library for event-driven programming in PHP. It has a huge number of featu ...

  6. 【转】Jmeter分布式压力测试

    安装 下载地址:http://jmeter.apache.org/download_jmeter.cgi 安装前提(因为jmeter依赖于Java所以必须先配置好java) 下载后解压: tar -x ...

  7. Hbase建模

    转自:http://blog.itpub.net/28912557/viewspace-1119865/ 什么情况下使用Hbase?1,成熟的数据分析主题,查询模式已经确定并且不易轻易改变.(主要还是 ...

  8. flume-kafka-storm日志处理经验

    转自:http://www.aboutyun.com/thread-9216-1-1.html 使用Storm处理事务型实时计算需求时的几处难点: http://blog.sina.com.cn/s/ ...

  9. Spring Annotation是怎么工作的?

    最近刚好看了下注解,虽然明白了注解的作用原理,但是仍然不明白Spring中的注解是如何工作的. 占座用,留待后续. 先来两个链接吧 https://dzone.com/articles/spring- ...

  10. port被占用的处理方法

    開始--执行--cmd 进入命令提示符 输入netstat -ano 就可以看到全部连接的PID 之后在任务管理器中找到这个PID所相应的程序假设任务管理器中没有PID这一项,能够在任务管理器中选&q ...