public class MyStack
{
Queue<int> Q = new Queue<int>(); /** Initialize your data structure here. */
public MyStack()
{ } /** Push element x onto stack. */
public void Push(int x)
{
Q.Enqueue(x);
} /** Removes the element on top of the stack and returns that element. */
public int Pop()
{
var tempQ = new Queue<int>(); var pop = -; while (Q.Count > )
{
var cur = Q.Dequeue(); if (Q.Count == )
{
pop = cur;
}
else
{
tempQ.Enqueue(cur);
}
} while (tempQ.Count > )
{
var cur = tempQ.Dequeue();
Q.Enqueue(cur);
} return pop;
} /** Get the top element. */
public int Top()
{
var tempQ = new Queue<int>(); var pop = -; while (Q.Count > )
{
var cur = Q.Dequeue();
if (Q.Count == )
{
pop = cur;
} tempQ.Enqueue(cur);
} while (tempQ.Count > )
{
var cur = tempQ.Dequeue();
Q.Enqueue(cur);
} return pop;
} /** Returns whether the stack is empty. */
public bool Empty()
{
return Q.Count == ;
}
} /**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.Push(x);
* int param_2 = obj.Pop();
* int param_3 = obj.Top();
* bool param_4 = obj.Empty();
*/

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. SSRS 在订阅的时候,在头值中找到无效的字符。将不重新发送邮件

    在头值中找到无效的字符.将不重新发送邮件 SSRS 在订阅的时候,在头值中找到无效的字符.将不重新发送邮件! 查看了一下,只要是发送文件类型的都不可以,改成HTML的就可以.然后重新把RS的报表文件友 ...

  2. New Concept English Two 8 19

    pls  practice every day $课文17 青春常驻 161. My aunt Jennifer is an actress. 我的姑姑詹妮弗是位演员, 162. She must b ...

  3. 在.NET开发中的单元测试工具之——NUnit

    NUnit介绍 NUnit是一个专门针对于.NET来写的单元测试框架,它是xUnit体系中的一员,在xUnit体系中还有针对Java的JUnit和针对C++的CPPUnit,在开始的时候NUnit和x ...

  4. jquery MD5

    /** * jQuery MD5 hash algorithm function * * <code> * Calculate the md5 hash of a String * Str ...

  5. python实践报错:SyntaxError: Non-ASCII character

    报错: File "C:\001myWorkspace\001myWork\workspace2\MyFirstPython\src\demo4\demo4-2.py", line ...

  6. 宏使用 Tricks

    人为地定义一些"无意义"的宏(宏名本身有意义),以起到提升代码程序的可读性. 1. IN/OUT 指定参数用于输入还是输出: #define IN #define OUT void ...

  7. Python itertools.combinations 和 itertools.permutations 等价代码实现

    最近编程时经常要用到排序组合的代码,想当年还抱着一些情况买了一本<组合数学>,不过现在这货也不知道被自己放哪里了,估计不会是垫桌子腿了吧. 由于去年去东北大学考博面试的时候遇到过可能涉及排 ...

  8. HDFS 和 YARN 的 HA 故障切换【转】

    来源:https://blog.csdn.net/u011414200/article/details/50336735 一 非 HDFS HA 集群转换成 HA 集群二 HDFS 的 HA 自动切换 ...

  9. IntelliJ IDEA 基础设置

    原文地址:IntelliJ IDEA 基础设置 博客地址:http://www.extlight.com 一.前言 IDEA 全称 IntelliJ IDEA,是java语言开发的集成环境,Intel ...

  10. CentOS 6.5 下搭建NTP服务器

    参考网站: http://www.iyunv.com/thread-64847-1-1.html http://acooly.iteye.com/blog/1993484 1         检查系统 ...