Implement the following operations of a stack using queues. (Easy)

  • 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).

分析:

使用两个队列实现,其中一个队列为空,用来进行颠倒顺序和输出队尾元素。

入栈操作: 向非空队列内入队即可

出栈操作:将非空队列除队尾元素外的所有元素导入另一个空队列,剩余队尾元素即为待应该待出栈元素

top()操作: 同出栈,注意只需访问返回,不需要让其出队,即仍需将其导入另一队列

注意:两队列地位平等,均可能用作储存和转移工作

代码:

 class Stack {
private:
queue<int> q1,q2;
public:
// Push element x onto stack.
void push(int x) {
if(!q1.empty()){
q1.push(x);
}
else{
q2.push(x);
}
} // Removes the element on top of the stack.
void pop() {
if(!q1.empty()){
while(q1.size() > ){
q2.push(q1.front());
q1.pop();
}
q1.pop();
}else{
while(q2.size() > ){
q1.push(q2.front());
q2.pop();
}
q2.pop();
}
} // Get the top element.
int top() {
if(!q1.empty()){
while(q1.size() > ){
q2.push(q1.front());
q1.pop();
}
int ans = q1.front();
q2.push(q1.front());
q1.pop();
return ans;
}else{
while(q2.size() > ){
q1.push(q2.front());
q2.pop();
}
int ans = q2.front();
q1.push(q2.front());
q2.pop();
return ans;
}
} // Return whether the stack is empty.
bool empty() {
if(q1.empty() && q2.empty()){
return true;
}
return false;
}
};
 

LeetCode225 Implement Stack using Queues的更多相关文章

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

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

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

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

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

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

  5. 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) -- ...

  6. [Swift]LeetCode225. 用队列实现栈 | Implement Stack using Queues

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

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

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

  8. Java for LeetCode 225 Implement Stack using Queues

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

  9. Implement Stack using Queues

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

随机推荐

  1. It\'s A Good Day To Die

    [00:01.82]Courage! Duty! Honor! [00:05.67]We call upon our troopers [00:07.90]In this our darkest ho ...

  2. JavaScript 实现回文解码

    题目也是源自今日头条前端工程师笔试题.题目描述: 现在有一个字符串,你要对这个字符串进行 n 次操作,每次操作给出两个数字:(p, l) 表示当前字符串中从下标为 p 的字符开始的长度为 l 的一个子 ...

  3. linux 修改命令提示符&ubuntu目录颜色个性化

    1.linux 命令提示符个性化 在 ANSI 兼容终端(例如 xterm.rxvt.konsole 等)里, 可以用彩色显示文本而不仅仅是黑白. 本文示范了如何使用粗体和彩色的文字.     相信每 ...

  4. python 读取xml文件

    首先,获得标签信息abc.xml <?xml version="1.0" encoding="utf-8"?> <catalog> &l ...

  5. quartz 通用的多线程定时任务

    TaskManager package mytest.task; import java.text.ParseException; import org.quartz.CronTrigger; imp ...

  6. 2019.9.28 csp-s模拟测试54 反思总结

    咕咕咕的冲动如此强烈x T1x: 看完题目想了想,感觉把gcd不为1的强行放在一组,看作一个连通块,最后考虑连通块之间的组合方式就可以了. 然后维护这个连通块可以写并查集可以连边跑dfs怎么着都行… ...

  7. npm ci命令比npm installer命令快2至10倍

    npm 5.7.1的发布给我们带了一系列新的功能. 其中我最喜欢的就是npm ci命令了. npm ci命令 1.npm ci命令只根据lock-file去下载node_modules. 如果你的pa ...

  8. ACdream 1101 线段树

    题目链接 瑶瑶想要玩滑梯 Time Limit: 10000/5000MS (Java/Others)Memory Limit: 512000/256000KB (Java/Others) Submi ...

  9. Ubuntu 12.04 安装 IQQ

    1. 安装 IQQ 首先应安装jdk包 2. 百度网盘下载: http://pan.baidu.com/share/home?uk=3071047022 3. 运行 (1) Linux用户给IQQ-1 ...

  10. hdu 3068 最长回文(manacher入门)

    最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...