LeetCode——Implement Queue using Stacks
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, andis emptyoperations 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的更多相关文章
- [LeetCode] Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- Leetcode Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- LeetCode Implement Queue using Stacks (数据结构)
题意: 用栈来实现队列. 思路: 一个栈是不够的,至少要两个. (1)插入.永远只插入到stack1中(插到栈顶). (2)弹出.如果stack2不为空,直接弹出stack2的栈顶,否则,将stack ...
- LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4
232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
- 【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( ...
- 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 ...
- Leetcode 232 Implement Queue using Stacks 和 231 Power of Two
1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
随机推荐
- 使用C#创建windows 服务
创建项目选择Windows Service 创建好项目之后,在生成的Service1.cs的设计页面右键选择Add Installer,会生成一个ProjectInstaller.cs.这个文件中有两 ...
- Python 字符串语法,for
#-*- coding:utf-8 -*- #字符串的拼接方式一 a = "hello" b = "world" c = a + " " + ...
- 登录centos虚拟机后显示-bash-4.1
http://zhidao.baidu.com/link?url=KwpGOdwFw1oxnL71pvPlfRgbRL_IuQeYRzIYJjiDb2SnX0dQye5yUXqHAGSyuD6u2nD ...
- android 内存管理机制、异常、垃圾回收
当 Android 应用程序退出时,并不清理其所占用的内存,Linux 内核进程也相应的继续存在,所谓“退出但不关闭”.从而使得用户调用程序时能够在第一时间得到响应. 当系统内存不足时,系统将激活内存 ...
- English Sentenses【no use】
1.Sorry if I might sound arrogant or offensive. 2.Any further question? 3.How dare you! 4.Try it if ...
- am335x watchdog
am335x watchdog 内核文档kernel/Documentation/watchdog Qt@aplex:~/kernel/7109/linux-3.2.0/Documentation/w ...
- webBrowser1.Document.Cookie取不到HttpOnly的Cookie,取Cookie不完整【转】
在做数据采集时,有些网站需要输入验证码,但各网站验证码都不同,不可能有完美的识别验证码的代码,所以我也没去研究,我所采取的方案是:在winform里通过WebBrowser调用网页先手动登录系统,然后 ...
- 巧用JS中的join方法操作字符串
1.将数组的元素组起一个字符串,以separator为分隔符,省略的话则用默认用逗号为分隔符 /** *把数组转换成特定符号分割的字符串 */ function arrayToString(arr,s ...
- 获取作为 URL 部署清单的位置。
ActivationContext ac = AppDomain.CurrentDomain.ActivationContext; ApplicationIdentity ai = ac.Identi ...
- js正则表达式的应用
JavaScript表单验证email,判断一个输入量是否为邮箱email,通过正则表达式实现. //检查email邮箱 function isEmail(str){ var reg = /^([a- ...