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 backpeek/pop from frontsize, and is empty operations 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的更多相关文章

  1. 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 ...

  2. [CareerCup] 3.5 Implement Queue using Two Stacks 使用两个栈来实现队列

    3.5 Implement a MyQueue class which implements a queue using two stacks. LeetCode上的原题,请参见我之前的博客Imple ...

  3. Implement Queue by Two Stacks

    As the title described, you should only use two stacks to implement a queue's actions. The queue sho ...

  4. LintCode Implement Queue by Two Stacks

    1. stack(先进后出): pop 拿出并返回最后值: peek 返回最后值: push 加入新值在后面并返回此值. 2. queue(先进先出) : poll = remove 拿出并返第一个值 ...

  5. lintcode :implement queue by two stacks 用栈实现队列

    题目 用栈实现队列 正如标题所述,你需要使用两个栈来实现队列的一些操作. 队列应支持push(element),pop() 和 top(),其中pop是弹出队列中的第一个(最前面的)元素. pop和t ...

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

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

  7. 【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( ...

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

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

  9. [LeetCode] Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

随机推荐

  1. GITHUB使用及入门总结

    这是我第一次应用git,以下仅供git的初学者参考.     github是一个基于git的代码托管平台,付费用户可以建私人仓库,我们一般的免费用户只能使用公共仓库,也就是代码要公开.这对于一般人来说 ...

  2. ubuntu 12.04下 ns3的下载 安装

    这个的内容我主要是参考了 http://blog.sina.com.cn/s/blog_7ec2ab360102wwsk.html 这个链接的学习,基本上过程没有出现的问题. 就是这个链接少了测试的一 ...

  3. VC2013一些感受

    这是一个我很早就在用的编译器,因为是微软官方的,极其高大上,安装包,界面错误的提示处理都相当简洁明了,不像VC6.0以及Codeblock太low了 但其实,我想说,我并不怎么用这玩意~就像Siri做 ...

  4. 共享服务Samba,实现liunx与Windows文件共享

    Samba服务程序 是一款SMB协议并有服务器和客户端组成的开源文件共享软件,实现了Linux 与Windows系统之间的文件共享 Samba的配置文件有太多注释的东西,为了方便使用下面的命令,可以更 ...

  5. Docker(七)-Dcoker常用命令

    容器生命周期管理 run start/stop/restart kill rm pause/unpause create exec 容器操作 ps inspect top attach events ...

  6. golang curl

    /** 1.可设置代理 2.可设置 cookie 3.自动保存并应用响应的 cookie 4.自动为重新向的请求添加 cookie */ package curl import ( "net ...

  7. Ubuntu使用dense_flow提取视频图像的光流图像

    使用dense_flow求取图像的光流图像,原项目地址: https://github.com/wanglimin/dense_flow 该方法使用的是opecnv最基本的光流图像计算方法,输出为 f ...

  8. 【bzoj3881】[Coci2015]Divljak AC自动机+树链的并+DFS序+树状数组

    题目描述 Alice有n个字符串S_1,S_2...S_n,Bob有一个字符串集合T,一开始集合是空的. 接下来会发生q个操作,操作有两种形式: “1 P”,Bob往自己的集合里添加了一个字符串P. ...

  9. js判断checkbox是否选中

    $('.div0 .checkbox1').prop('checked')选中返回 true未选中返回 false $('.div0').prop("checked", true) ...

  10. 【CF912E】Prime Game(meet in the middle)

    [CF912E]Prime Game(meet in the middle) 题面 CF 懒得翻译了. 题解 一眼题. \(meet\ in\ the\ middle\)分别爆算所有可行的两组质数,然 ...