Implement Queue by Two Stacks & Implement Stack using Queues
Implement Queue by Two Stacks
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.
class MyQueue {
private Stack<Integer> stack1, stack2;
public MyQueue() {
// do initialization if necessary
stack1 = new Stack<Integer>();
stack2 = new Stack<Integer>();
}
// 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() {
if (stack2.isEmpty()) {
while(!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
stack2.pop();
}
// Get the front element.
public int peek() {
if (stack2.isEmpty()) {
while(!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
return stack2.peek();
}
// Return whether the queue is empty.
public boolean empty() {
return stack1.isEmpty() && stack2.isEmpty();
}
}
Implement Stack using Queues
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 front,size, andis emptyoperations 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).
分析:
用两个queues,第二个queue永远为空,它只用于临时保存数字而已。
class MyStack {
// Push element x onto stack.
LinkedList<Integer> q1 = new LinkedList<Integer>();
LinkedList<Integer> q2 = new LinkedList<Integer>();
public void push(int x) {
q1.offer(x);
}
// Removes the element on top of the stack.
public void pop() {
if (!empty()) {
while(q1.size() >= ) {
q2.offer(q1.poll());
}
q1.poll();
while(q2.size() >= ) {
q1.offer(q2.poll());
}
}
}
// Get the top element.
public int top() {
int value = ;
if (!empty()) {
while(q1.size() >= ) {
if (q1.size() == ) {
value = q1.peek();
}
q2.offer(q1.poll());
}
while(q2.size() >= ) {
q1.offer(q2.poll());
}
}
return value;
}
// Return whether the stack is empty.
public boolean empty() {
return q1.size() == ;
}
}
Implement Queue by Two Stacks & Implement Stack using Queues的更多相关文章
- 40. Implement Queue by Two Stacks【medium】
As the title described, you should only use two stacks to implement a queue's actions. The queue sho ...
- [CareerCup] 3.5 Implement Queue using Two Stacks 使用两个栈来实现队列
3.5 Implement a MyQueue class which implements a queue using two stacks. LeetCode上的原题,请参见我之前的博客Imple ...
- Implement Queue by Two Stacks
As the title described, you should only use two stacks to implement a queue's actions. The queue sho ...
- LintCode Implement Queue by Two Stacks
1. stack(先进后出): pop 拿出并返回最后值: peek 返回最后值: push 加入新值在后面并返回此值. 2. queue(先进先出) : poll = remove 拿出并返第一个值 ...
- lintcode :implement queue by two stacks 用栈实现队列
题目 用栈实现队列 正如标题所述,你需要使用两个栈来实现队列的一些操作. 队列应支持push(element),pop() 和 top(),其中pop是弹出队列中的第一个(最前面的)元素. pop和t ...
- 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( ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
随机推荐
- Linux内核分析第三周学习笔记
linux内核分析第三周学习笔记 标签(空格分隔): 20135328陈都 陈都 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.co ...
- 一个java实现的简单的4则运算器
有些基础知识有欠缺,补一下,顺便练习一下java import com.sun.deploy.util.ArrayUtil; import java.util.*; public class Main ...
- 四则运算level2
package j; import java.util.Scanner; public class Main { public static void main(String[] args) { Sc ...
- java 多维数据定义
//一维数组定义与输出class less02{ public static void main(String[] args) { int stu[]=new int[]{1,2 ...
- 组件 -- Button
.btn --------------------------------- button的背景色: .btn-primary .btn-success .btn-secondary .btn-dan ...
- 基于 Redis 做分布式锁
基于 REDIS 的 SETNX().EXPIRE() 方法做分布式锁 setnx() setnx 的含义就是 SET if Not Exists,其主要有两个参数 setnx(key, value) ...
- [转帖]SQLSERVER 使用触发器实现 禁用sa用户 在非本机登录
原贴地址: https://blog.csdn.net/reblue520/article/details/51580102 具体的方法为: 创建一个触发器 CREATE TRIGGER forbid ...
- Helm 安装 wordpress
1. 前置需要安装 storageclass 然后 安装helm 客户端 helm tiller 服务端 2. 设置 当前的位阿里云的 repo 3. 查找 wordpress的镜像 helm sea ...
- CAS单点登录的时候出现票根'ST-xxxxxx-cas'不符合目标服务
CAS单点登录遇到问题:票根'ST-xxxxxx-cas'不符合目标服务,原因出在linux 时间未同步,差了3分钟 .
- hdwiki 前后台版权信息在哪修改
hdwiki 前台copyright 信息在 view/default/footer.htm 搜索footer-phdwiki 后台copyright 信息在 view/default/admin_m ...