1. public class MyQueue
  2. {
  3. Stack<int> S = new Stack<int>();
  4. /** Initialize your data structure here. */
  5. public MyQueue()
  6. {
  7.  
  8. }
  9.  
  10. /** Push element x to the back of queue. */
  11. public void Push(int x)
  12. {
  13. S.Push(x);
  14. }
  15.  
  16. /** Removes the element from in front of queue and returns that element. */
  17. public int Pop()
  18. {
  19. List<int> list = new List<int>();
  20. var x = -;
  21. while (S.Count > )
  22. {
  23. x = S.Pop();
  24. list.Add(x);
  25. }
  26.  
  27. for (int i = list.Count - ; i >= ; i--)
  28. {
  29. S.Push(list[i]);
  30. }
  31.  
  32. return x;
  33. }
  34.  
  35. /** Get the front element. */
  36. public int Peek()
  37. {
  38. if (S.Count > )
  39. {
  40. return S.ElementAt(S.Count - );
  41. }
  42. else
  43. {
  44. return -;
  45. }
  46. }
  47.  
  48. /** Returns whether the queue is empty. */
  49. public bool Empty()
  50. {
  51. return S.Count == ;
  52. }
  53. }
  54.  
  55. /**
  56. * Your MyQueue object will be instantiated and called as such:
  57. * MyQueue obj = new MyQueue();
  58. * obj.Push(x);
  59. * int param_2 = obj.Pop();
  60. * int param_3 = obj.Peek();
  61. * bool param_4 = obj.Empty();
  62. */

https://leetcode.com/problems/implement-queue-using-stacks/#/description

leetcode232的更多相关文章

  1. [Swift]LeetCode232. 用栈实现队列 | Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  2. 【LeetCode232】 Implement Queue using Stacks★

    1.题目描述 2.思路 思路简单,这里用一个图来举例说明: 3.java代码 public class MyQueue { Stack<Integer> stack1=new Stack& ...

  3. LeetCode232:Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) – Push element x to the back of ...

  4. LeetCode232 Implement Queue using Stacks Java 题解

    题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the bac ...

  5. LeetCode232 用栈实现队列

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

  6. 面试之leetcode20堆栈-字符串括号匹配,队列实现栈

    1 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合.左括号必须以正确的顺序闭合.注意空字符串可被认 ...

  7. LeetCode-Stack-Easy

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

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

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

随机推荐

  1. 一. Spring框架防XXS跨站攻击

    使用 Spring 框架进行 Java Web 开发,可以在 web.xml 文件中设置 HTML encode,在 JSP 文件页面元素 form 中确定实施. web.xml 加上: <co ...

  2. 快速理解 FastCGI、PHP-CGI、PHP-FPM

    你(PHP)去和泰国人(web服务器,如 Apache.Nginx)谈生意 你说中文(PHP代码),他说泰语(C代码),互相听不懂,怎么办?那就都把各自说的话转换成英语(FastCGI 协议)吧. 怎 ...

  3. Digitalocean+DNSPod搭建Meteor.js博客Telescope.js

    1. 什么是Meteor.js 基于Node.js的一个快速开发平台. 简言之,Node.js>Meteor.js 对等于Ruby>Ruby on Rails的关系. 官网:http:// ...

  4. Work Queues(点对多)

    Work Queues(点对多) 多个消费者在同一个消息队列中获取消息的情况.在有些应用当中,消费端接收到消息任务需要长时间的处理,如果等上一个消息处理完成以后再取下一个数据进行处理的话,势必会有一些 ...

  5. LNMP架构下的nginx、mysql、php的源码安装

    一.LNMP的介绍 LNMP就是Linux+Nginx+Mysql+Php这种网站服务架构.Linux是一类Unix计算机操作系统的统称,是目前最流行的免费操作系统,常见版本有:centos.ubun ...

  6. eclipse加入c标签

    在MyEclipse中使用jstl标签只需导读jstl.jar就能使用,但是在Eclipse中还需要一点小套路 步骤: 一.导入jstl.jar 二.导入导入standard.jar 三.在WEB-I ...

  7. springboot---数据整合篇

    本文讲解 Spring Boot 基础下,如何使用 JDBC,配置数据源和通过 JdbcTemplate 编写数据访问. 环境依赖 修改 POM 文件,添加spring-boot-starter-jd ...

  8. 使用RESTful风格整合springboot+mybatis

    说明: 本文是springboot和mybatis的整合,Controller层使用的是RESTful风格,数据连接池使用的是c3p0,通过postman进行测试 项目结构如下: 1.引入pom.xm ...

  9. WIN 10环境下JDK的安装和环境配置

    在做测试的过程中,诸如Selenium.Appium.Macaca.Airtest.RobotFramework.Jmeter等框架或工具都需要用到一样基础的环境JAVA JDK.最近刚好换了电脑,就 ...

  10. Convex 一道阿姆斯特朗回旋好题

    2001年5月8日,阿姆斯特朗(Armstrong, 1929-2013) 教授发明了一种名为“阿姆斯特朗回旋加速喷气式阿姆斯特朗加密”的加密算法,算法从未公开,直至2013阿姆斯特朗教授逝世后,其生 ...