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()操作: 同出栈,注意只需访问返回,不需要让其出队,即仍需将其导入另一队列

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

代码:

  1. class Stack {
  2. private:
  3. queue<int> q1,q2;
  4. public:
  5. // Push element x onto stack.
  6. void push(int x) {
  7. if(!q1.empty()){
  8. q1.push(x);
  9. }
  10. else{
  11. q2.push(x);
  12. }
  13. }
  14.  
  15. // Removes the element on top of the stack.
  16. void pop() {
  17. if(!q1.empty()){
  18. while(q1.size() > ){
  19. q2.push(q1.front());
  20. q1.pop();
  21. }
  22. q1.pop();
  23. }else{
  24. while(q2.size() > ){
  25. q1.push(q2.front());
  26. q2.pop();
  27. }
  28. q2.pop();
  29. }
  30. }
  31.  
  32. // Get the top element.
  33. int top() {
  34. if(!q1.empty()){
  35. while(q1.size() > ){
  36. q2.push(q1.front());
  37. q1.pop();
  38. }
  39. int ans = q1.front();
  40. q2.push(q1.front());
  41. q1.pop();
  42. return ans;
  43. }else{
  44. while(q2.size() > ){
  45. q1.push(q2.front());
  46. q2.pop();
  47. }
  48. int ans = q2.front();
  49. q1.push(q2.front());
  50. q2.pop();
  51. return ans;
  52. }
  53. }
  54.  
  55. // Return whether the stack is empty.
  56. bool empty() {
  57. if(q1.empty() && q2.empty()){
  58. return true;
  59. }
  60. return false;
  61. }
  62. };
 

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. ECMAScript 5 新增 Object 接口

    对象 构造器 说明 Object getPrototypeOf 返回对象的原型 Object getOwnPropertyDescriptor 返回对象自有属性的属性描述符 Object getOwn ...

  2. python-基础-字符串-列表-元祖-字典2

    接上:http://www.cnblogs.com/liu-wang/p/8973273.html 3 元组 4 字典 4.1 字典的介绍 <2>软件开发中的字典 变量info为字典类型: ...

  3. Leetcode102. Binary Tree Level Order Traversal二叉树的层次遍历

    给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回其 ...

  4. 常用web字体的使用指南

    而真正的挑战在于中文字体,由于中文字体组成的特殊性导致其体积过于庞大,除了操作系统内置的字体之外,我们很难在网站上应用其他的字体.在可选性很差的前提之下,如何正确的使用中文字体呢? 首先,以下的字体声 ...

  5. 关于JavaScript的43道题①

    最近在github上大火的43到js代码题,有很多人搬运.原链接https://github.com/lydiahallie/javascript-questions 1.下面代码的输出是什么? fu ...

  6. 使用setTimeout函数解决栈溢出问题

    下面的代码,如果队列太长会导致栈溢出,怎样解决这个问题并且依然保持循环部分: var list = readHugeList(); var nextListItem = function() { va ...

  7. Hackerrank--Savita And Friends(最小直径生成树MDST)

    题目链接 After completing her final semester, Savita is back home. She is excited to meet all her friend ...

  8. VMware 安装 ubuntu 后安装 VMWare tools

    1.如果 VMware 的安装 VMWare tools 的菜单是灰色, 很可能原因是:    你的 cdrom 被占用着. 关闭系统, 编辑配置, 把cdrom 改为 自动检测. 即不要开始就加载一 ...

  9. Oracle中函数如何返回结果集

    在Oracle中,用函数返回结果集有时候要用到,下面是demo: 1 2 3 4 5 6 7 create or replace type t_test as object ( id integer, ...

  10. python模拟浏览器文件上传,csrf放行

    服务器端视图函数 from django.shortcuts import render,HttpResponse from django.views.decorators.csrf import c ...