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. DeepID1,DeepID2

    1.DeepID1 (Deep Learning Face Representation from Predicting 10,000 Classes) Step1:构建网络框架 DeepConvNe ...

  2. Alpha 答辩总结

    前言 作业发布 组长 成员 贡献比例 ★ 530 雨勤 23% 311 旭 23% 403 俊 18% 223 元 23% 437 海辉 13% 10天 Alpha 冲刺站立会议博客链接汇总 Alph ...

  3. Beta冲刺——day6

    Beta冲刺--day6 作业链接 Beta冲刺随笔集 github地址 团队成员 031602636 许舒玲(队长) 031602237 吴杰婷 031602220 雷博浩 031602134 王龙 ...

  4. Final发布点评

    1.  约跑App——nice!:为改进演示效果,本组使用摄像头实时采集投影的方式展示其作品,是一种演示的创新.本组重点放在了修改上次来着其他组发现的bug,不过新功能上好像没有加入多少,可能是保证软 ...

  5. [转帖] k8s dashboard 的创建 升级 以及 admin token的创建和简单使用.

    Kubernetes Dashboard中的身份认证详解 https://jimmysong.io/posts/kubernetes-dashboard-upgrade/ Thu Nov 2, 201 ...

  6. 怎样实现在DBGrid中双击选择整行,并且可以多选?谢谢!!

    DBGrid1->Options里有个dgMultiSelect,把它设为true就能多选了 先设置DBGrid1->options中dgRowSelect = true, dgMulti ...

  7. 当数据库字段与model字段规则不一致时候 需要在xml里面手工转换

  8. Linux Deploy Ubuntu安装MySQL

    一.在Android手机安装Linux 二.Ubuntu安装Mysql 建议在root用户上操作 sudo su 输入密码 (一)安装mysql 1. sudo apt-get install mys ...

  9. Trips CodeForces - 1037E(思维dfs)

    题意: 就是几个人去旅游,组队的条件是对于某个队员 队里至少有两个是他的朋友,每天早晨都会有一对新人成为朋友 解析: 用set标记互为朋友 a[i] b[i] 表示在第i天早晨 u和v成为朋友 先求最 ...

  10. BZOJ刷题指南(转)

    基础(65) 巨水无比(4):1214.3816:2B题:1000A+B:2462:输出10个1 模拟/枚举/暴力(15):4063傻子模拟:1968小学生暴力:1218前缀和暴力:3856读英文:4 ...