public class MyQueue
{
Stack<int> S = new Stack<int>();
/** Initialize your data structure here. */
public MyQueue()
{ } /** Push element x to the back of queue. */
public void Push(int x)
{
S.Push(x);
} /** Removes the element from in front of queue and returns that element. */
public int Pop()
{
List<int> list = new List<int>();
var x = -;
while (S.Count > )
{
x = S.Pop();
list.Add(x);
} for (int i = list.Count - ; i >= ; i--)
{
S.Push(list[i]);
} return x;
} /** Get the front element. */
public int Peek()
{
if (S.Count > )
{
return S.ElementAt(S.Count - );
}
else
{
return -;
}
} /** Returns whether the queue is empty. */
public bool Empty()
{
return S.Count == ;
}
} /**
* 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://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. OSI七层与tcp/ip四层

    1)OSI七层模型 OSI中的层 功能 TCP/IP协议族 应用层 文件传输,电子邮件,文件服务,虚拟终端 TFTP,HTTP,SNMP,FTP,SMTP,DNS,Telnet 表示层 数据格式化,代 ...

  2. get方法传送中文乱码解决方法

    找到tomcat配置文件 server.xml 找到<Connector port="8080" .......  />   (......为配置文件中原来内容) 在最 ...

  3. pycharm 教程(一)安装和首次使用

    PyCharm 是我用过的Python编辑器中,比较顺手的一个.而且可以跨平台,在 mac 和 windows 下面都可以用,这点比较好. 首先预览一下 PyCharm 在实际应用中的界面:(更改了P ...

  4. Java语言发展史和eclipse配置

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  5. epoll模型边沿触发

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  6. New Concept English Two 25 67

    $课文65  小象对警察 683. Last Christmas, the circus owner, Jimmy Gates, decided to take some presents to a ...

  7. Oracle 分析函数及常用函数

    什么叫分析函数(Analytic function)? Oracle从8.1.6开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是 对于每个组返回多行,而聚合函数对于每个组 ...

  8. ASP.NET CORE网站部署到 windows server 的IIS 上去

    章基于我自己经验的一个总结,在windows服务器上部署asp.net core网站.环境是 windows server 2012数据中心版本 第一步先安装 IIS 服务器 接下来就是一路下一步,然 ...

  9. 實驗項目wordcount

    wordcount 1.设计思路 第一步 :主函数参数使用命令行参数,定义一个文件指针fp. 第二步:判断能否用只读的形式打开命令行指针中的文件,并让指针指向打开函数,若不能则输出不能读取文件,否则下 ...

  10. HDU3488Tour (KM算法)

    题意:   有N个点,M个单向边,现在要你设计N条路线覆盖所有的点,每个点都属于且值属于一个环.(为什么是N条边:和最小生成树为什么有N-1条边是一样的证明). 解析:  每个点都有一个喜欢对象(出度 ...