目录

  • 题目描述:
  • 示例:
  • 说明:
  • 解法:

题目描述:

使用栈实现队列的下列操作:

  • push(x) -- 将一个元素放入队列的尾部。
  • pop() -- 从队列首部移除元素。
  • peek() -- 返回队列首部的元素。
  • empty() -- 返回队列是否为空。

示例:

    MyQueue queue = new MyQueue();queue.push(1);queue.push(2);  queue.peek();  // 返回 1queue.pop();   // 返回 1queue.empty(); // 返回 false

说明:

  • 你只能使用标准的栈操作 -- 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
  • 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
  • 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。

解法:

class MyQueue {
public:/** Initialize your data structure here. */stack<int> stk;MyQueue() {}/** Push element x to the back of queue. */void push(int x) {stk.push(x);}/** Removes the element from in front of queue and returns that element. */int pop() {stack<int> tmp;while(!stk.empty()){tmp.push(stk.top());stk.pop();}int res = tmp.top();tmp.pop();while(!tmp.empty()){stk.push(tmp.top());tmp.pop();}return res;}/** Get the front element. */int peek() {stack<int> tmp;while(!stk.empty()){tmp.push(stk.top());stk.pop();}int res = tmp.top();while(!tmp.empty()){stk.push(tmp.top());tmp.pop();}return res;}/** Returns whether the queue is empty. */bool empty() {return stk.empty();}
};/*** Your MyQueue object will be instantiated and called as such:* MyQueue obj = new MyQueue();* obj.push(x);* int param_2 = obj.pop();* int param_3 = obj.peek();* bool param_4 = obj.empty();*/

转载于:https://www.cnblogs.com/zhanzq/p/10571156.html

leetcode 232. 用栈实现队列(Implement Queue using Stacks)相关推荐

  1. 使用栈实现队列 Implement Queue using Stacks

    为什么80%的码农都做不了架构师?>>>    问题: Implement the following operations of a queue using stacks. pus ...

  2. LeetCode 232. 用栈实现队列(双栈法-队列)

    1. 题目 使用栈实现队列的下列操作: push(x) – 将一个元素放入队列的尾部. pop() – 从队列首部移除元素. peek() – 返回队列首部的元素. empty() – 返回队列是否为 ...

  3. Leetcode 232. 用栈实现队列 解题思路及C++实现

    解题思路: 使用两个栈,一个栈a用来存储每一次操作得到的队列,另一个栈b作为辅助栈. 在每一次push操作的时候,先把a中排好的队列(先进先出),依次push进栈b,所以,栈b中元素的排序就是后进先出 ...

  4. leetcode 232. 用栈实现队列 思考分析

    题目 请你仅使用两个栈实现先入先出队列.队列应当支持一般队列的支持的所有操作(push.pop.peek.empty): 实现 MyQueue 类: void push(int x) 将元素 x 推到 ...

  5. 【Java】LeetCode 232. 用栈实现队列

    题目描述 : 请你仅使用两个栈实现先入先出队列.队列应当支持一般队列支持的所有操作(push.pop.peek.empty): 实现 MyQueue 类: void push(int x) 将元素 x ...

  6. 代码随想录【day 10 栈与队列】| 232.用栈实现队列、 225. 用队列实现栈

    代码随想录[day 10 栈与队列]| 232.用栈实现队列. 225. 用队列实现栈 理论基础 LeetCode 232.用栈实现队列 题目链接:232.用栈实现队列 卡哥文解 视频讲解 解题思路( ...

  7. LeetCode刷题记录12——232. Implement Queue using Stacks(easy)

    LeetCode刷题记录12--232. Implement Queue using Stacks(easy) 目录 LeetCode刷题记录12--232. Implement Queue usin ...

  8. 【Leetcode】232.用栈实现队列

    题目链接:232.用栈实现队列 关键字:栈.队列 解题思路 使用两个栈实现队列,其中一个栈正常使用,另一个栈用来保存逆序数据 代码实现 class MyQueue { public:/** Initi ...

  9. Leetcode刷题——栈与队列

    Leetcode刷题--栈与队列 一.栈与队列定义 二.Leetcode题目 1.用栈实现队列 2.用队列实现栈 一.栈与队列定义 栈:先进后出,栈提供push 和 pop 等等接口,所有元素必须符合 ...

最新文章

  1. docker linux 快速开窗口_Linux快速搭建Docker-阿里云开发者社区
  2. python【力扣LeetCode算法题库】3- 无重复字符的最长子串
  3. day05python
  4. 软件项目组织管理(一)项目管理概述
  5. r510服务器开机无显示,联智通达工业主板常见问题之工控电脑开机无显示
  6. 【华为云技术分享】处理器存储模型概述(1)
  7. ceb2Pdf.exe实现ceb文件转pdf,内网离线安装wine(手动编译32位)实现在centos(linux)上运行window程序exe程序,解决wine : Z:\xxx EXE格式无效
  8. 初步熟悉RHEL 8
  9. 无聊开始玩路由器,入门Tomato固件
  10. django_jquery_ajax二级联动菜单
  11. 删除有外键关联的表报错,Cannot delete or update a parent row: a foreign key constraint fails
  12. python鼠标点击事件event_opencv-python教程学习系列5-处理鼠标事件
  13. 我深爱的Java,对不起,我出轨了!!!呸!渣男!
  14. API接口安全思考和最佳实践
  15. 数据分析-思维分析逻辑day01
  16. HTC Vive 手柄
  17. GDP越高就越幸福吗?用Python分析《世界幸福指数报告》后我们发现…
  18. 发布WebGL遇到的问题
  19. 从来只留一场梦 一场春梦成白头 上软疑似解体 忆仙剑而作
  20. 《安富莱嵌入式周报》第276期:2022.07.25--2022.07.31

热门文章

  1. IOS端与Java端MD5加密方法
  2. Hdu - 2647 - Reward
  3. 【恋上数据结构】复杂度知识以及LeetCode刷题指南
  4. 软件设计师24-算法
  5. 计算机出现黑屏问题方法派出,电脑重装系统开机常见黑屏问题的解决方法
  6. 报表性能优化方案之多数据集实现层式报表
  7. spring中怎么访问MySQL过程_DB数据源之SpringBoot+MyBatis踏坑过程(六)mysql中查看连接,配置连接数量...
  8. limit mysql 取最后_更优美的 limit 使用方法 (limit 对 mysql 数据查询的性能影响)
  9. h5外卖源码php_从零搭建外卖CPS平台小程序开发
  10. ue4打包安卓发送udp报文_内核udp报文截取、修改和发送