1. public class MyStack
  2. {
  3. Queue<int> Q = new Queue<int>();
  4.  
  5. /** Initialize your data structure here. */
  6. public MyStack()
  7. {
  8.  
  9. }
  10.  
  11. /** Push element x onto stack. */
  12. public void Push(int x)
  13. {
  14. Q.Enqueue(x);
  15. }
  16.  
  17. /** Removes the element on top of the stack and returns that element. */
  18. public int Pop()
  19. {
  20. var tempQ = new Queue<int>();
  21.  
  22. var pop = -;
  23.  
  24. while (Q.Count > )
  25. {
  26. var cur = Q.Dequeue();
  27.  
  28. if (Q.Count == )
  29. {
  30. pop = cur;
  31. }
  32. else
  33. {
  34. tempQ.Enqueue(cur);
  35. }
  36. }
  37.  
  38. while (tempQ.Count > )
  39. {
  40. var cur = tempQ.Dequeue();
  41. Q.Enqueue(cur);
  42. }
  43.  
  44. return pop;
  45. }
  46.  
  47. /** Get the top element. */
  48. public int Top()
  49. {
  50. var tempQ = new Queue<int>();
  51.  
  52. var pop = -;
  53.  
  54. while (Q.Count > )
  55. {
  56. var cur = Q.Dequeue();
  57. if (Q.Count == )
  58. {
  59. pop = cur;
  60. }
  61.  
  62. tempQ.Enqueue(cur);
  63. }
  64.  
  65. while (tempQ.Count > )
  66. {
  67. var cur = tempQ.Dequeue();
  68. Q.Enqueue(cur);
  69. }
  70.  
  71. return pop;
  72. }
  73.  
  74. /** Returns whether the stack is empty. */
  75. public bool Empty()
  76. {
  77. return Q.Count == ;
  78. }
  79. }
  80.  
  81. /**
  82. * Your MyStack object will be instantiated and called as such:
  83. * MyStack obj = new MyStack();
  84. * obj.Push(x);
  85. * int param_2 = obj.Pop();
  86. * int param_3 = obj.Top();
  87. * bool param_4 = obj.Empty();
  88. */

https://leetcode.com/problems/implement-stack-using-queues/#/description

leetcode225的更多相关文章

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

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

  2. 【LeetCode225】 Implement Stack using Queues★

    1.题目 2.思路 3.java代码 import java.util.LinkedList; import java.util.Queue; public class MyStack { priva ...

  3. Leetcode225 用栈实现队列

    大众思路: 用两个栈实现,记为s1,s2 1.元素入栈时,加入s1 2.元素出栈时,对s2进行判断,如果s2为空,则将全部s1元素弹出并压入到s2,然后从s2栈顶弹出一个元素:如果s2不为空,则直接从 ...

  4. LeetCode225 Implement Stack using Queues

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

  5. LeetCode225 用队列实现栈

    使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 注意: 你只能使用队列的基本操作 ...

  6. LeetCode-Stack-Easy

    简单题 1. 有效的括号(leetcode-20) 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 1. 左括号必须用相同类型的右括 ...

  7. LeetCode通关:栈和队列六连,匹配问题有绝招

    刷题路线参考: https://github.com/chefyuan/algorithm-base https://github.com/youngyangyang04/leetcode-maste ...

随机推荐

  1. ActiveStorage. 英文书Learnrails5.2的案例,看如何放到云上。

    ActiveStorage. 英文书Learnrails5.2的案例 本例子目标:增加一个avatar image给每个用户. 准备: 需要安装Imagemagick software.它可以crea ...

  2. hdu4280网络流之dinic

    这题就是个模板题,不过我是第一次写dinic,好久没用链式前向星又不会了... 时间:9126ms #include<map> #include<set> #include&l ...

  3. python学习笔记(一)---python下载以及环境的安装

    转载网址:https://www.runoob.com/python/python-install.html 1.下载python安装包: 安装包下载网址(如下图所在的网址):https://www. ...

  4. vue.js 源代码学习笔记 ----- text-parse.js

    /* @flow */ import { cached } from 'shared/util' import { parseFilters } from './filter-parser' //找到 ...

  5. Linux DMA Engine framework(2)_功能介绍及解接口分析

    http://www.wowotech.net/linux_kenrel/dma_engine_api.html 补充 http://www.zhimengzhe.com/linux/259646.h ...

  6. RF/GBDT/XGBoost/LightGBM简单总结(完结)

    这四种都是非常流行的集成学习(Ensemble Learning)方式,在本文简单总结一下它们的原理和使用方法. Random Forest(随机森林): 随机森林属于Bagging,也就是有放回抽样 ...

  7. tf.device()指定tensorflow运行的GPU或CPU设备

    在tensorflow中,我们可以使用 tf.device() 指定模型运行的具体设备,可以指定运行在GPU还是CUP上,以及哪块GPU上. 设置使用GPU 使用 tf.device('/gpu:1' ...

  8. php 递归读取目录

    看到很多面试题有这个,今天有机会写了一下. 要注意的是: 在opendir这个函数用完后,要注意closedir,因为安全问题,打开的目录依然存在于内存中,在并发情况下最好关闭,不然容易被破坏. &l ...

  9. 用 dotTrace 进行性能分析时,各种不同性能分析选项的含义和用途

    对 .NET 程序进行性能分析,dotTrace 能应对绝大多数的场景.在开启一个进程进行性能分析之前,我们会看到一些性能分析选项(Profiler Options).本文将介绍这几个选项的含义,并用 ...

  10. .NET/C# 在代码中测量代码执行耗时的建议(比较系统性能计数器和系统时间)

    我们有很多种方法评估一个方法的执行耗时,比如使用性能分析工具,使用基准性能测试.不过传统的在代码中编写计时的方式依然有效,因为它可以生产环境或用户端得到真实环境下的执行耗时. 如果你希望在 .NET/ ...