LeetCode Implement Queue using Stacks (数据结构)
题意:
用栈来实现队列。
思路:
一个栈是不够的,至少要两个。
(1)插入。永远只插入到stack1中(插到栈顶)。
(2)弹出。如果stack2不为空,直接弹出stack2的栈顶,否则,将stack1中的所有元素转移到stack2中,栈顶自然就是队头了,再弹出。
(3)返回队头。与(2)一样。
(4)是否为空。判断两个栈是否同时为空即可。
只要保证stack2为空时才可以将stack1转移到stack2中,就可以保证两边并不会产生混乱而出错。
1 class Queue { 2 /* 3 // Push element x to the back of queue. 4 void push(int x) { 5 6 } 7 8 // Removes the element from in front of queue. 9 void pop(void) { 10 11 } 12 13 // Get the front element. 14 int peek(void) { 15 16 } 17 18 // Return whether the queue is empty. 19 bool empty(void) { 20 21 } 22 */ 23 stack<int> stack1,stack2; 24 public: 25 bool change()//当stack2为空时,将stack1转到stack2中 26 { 27 while(!stack1.empty()) 28 { 29 stack2.push(stack1.top()); 30 stack1.pop(); 31 } 32 if(stack2.empty()) return true; 33 else return false; 34 } 35 void push(int x) 36 { 37 stack1.push(x); 38 } 39 void pop(void) 40 { 41 if(!stack2.empty()) stack2.pop(); 42 else if(!change()) stack2.pop(); 43 } 44 int peek(void) 45 { 46 if(!stack2.empty()) return stack2.top(); 47 else 48 { 49 if(!change()) return stack2.top(); 50 return 0; 51 } 52 } 53 bool empty(void) 54 { 55 if(stack1.empty()&&stack2.empty()) return true; 56 else return false; 57 } 58 };
AC代码
转载于:https://www.cnblogs.com/xcw0754/p/4927606.html
LeetCode Implement Queue using Stacks (数据结构)相关推荐
- LeetCode刷题记录12——232. Implement Queue using Stacks(easy)
LeetCode刷题记录12--232. Implement Queue using Stacks(easy) 目录 LeetCode刷题记录12--232. Implement Queue usin ...
- Lintcode: Implement Queue by Stacks 解题报告
Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...
- leetcode python3 简单题232. Implement Queue using Stacks
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百三十二题 (1)题目 英文: Implement the following ...
- LeetCode 232. Implement Queue using Stacks
题目: Implement the following operations of a queue using stacks. push(x) – Push element x to the back ...
- C#LeetCode刷题之#232-用栈实现队列(Implement Queue using Stacks)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4108 访问. 使用栈实现队列的下列操作: push(x) -- ...
- 使用栈实现队列 Implement Queue using Stacks
为什么80%的码农都做不了架构师?>>> 问题: Implement the following operations of a queue using stacks. pus ...
- leetcode 232. 用栈实现队列(Implement Queue using Stacks)
目录 题目描述: 示例: 说明: 解法: 题目描述: 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列 ...
- LeetCode 232. Implement Queue using Stacks--用2个栈来实现一个队列--C++解法
LeetCode 232. Implement Queue using Stacks–C++解法 LeetCode题解专栏:LeetCode题解 我做的所有的LeetCode的题目都放在这个专栏里,大 ...
- [CareerCup] 3.5 Implement Queue using Two Stacks 使用两个栈来实现队列
3.5 Implement a MyQueue class which implements a queue using two stacks. LeetCode上的原题,请参见我之前的博客Imple ...
最新文章
- Spring --- SpEL
- 专题 14 IPC之共享内存
- python安装包之后仍提示_解决已经安装requests,却依然提示No module named requests问题...
- Google视角的地理信息科学家
- 丁奇 mysql_丁奇-MySQL实战读书笔记4
- “javax.servlet.http.HttpServlet“ 错误
- Django初次体验
- android js交互 数组,Android WebView —— Java 与 JavaScript 交互总结
- 【自用】docker命令记录
- Scala 循环(打断嵌套循环)
- 中国渔船数量不断下降,海洋渔船下降速度较慢「图」
- 基于掷色子规则的c语言编程,掷骰子游戏-C语言
- 使用pygame制作Flappy bird小游戏
- 用 Python 写软件原来这么简单!
- word文档doc格式转换成docx
- 华为,其实你的对手是时代
- git pull 和 git fecth 的区别
- 微信开发者工具字体模糊
- MySQL数据同步到ES集群(MySQL数据库与ElasticSearch全文检索的同步)
- IBM MQ常用命令
热门文章
- jer中无html文件,index.html
- k8s pod MySQL环境变量,如何使用Kubernetes的configmap通过环境变量注入到pod里
- 后端技术:Java编程中忽略这些细节,Bug肯定少不了
- 安卓扫一扫自动跳转软件_ipad软件使用技巧
- 更新系统后魔兽世界无法连接至服务器,《魔兽世界:熊猫人之谜》5.0launcher更新无法连接服务器解决方法...
- Java容器坐标起点_Java的屏幕坐标是以像素为单位,容器的左下角被确定为坐标的起点...
- linux内核源码树
- js,java时间处理
- 0418 jQuery笔记(添加事件、each、prop、$(this))
- Cloudstack系统配置(三)