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. python实现:将文本文件分割成多个小文本文件(php也可实现)

    前两天有个朋友说,想实现一个文本文件按照固定行数进行分割成多个文本文件,却不知如何实现.如果数据量小手动分割下就好了,如果数据量很大的话手动完成实在太耗费人力了,也不现实.那么就需要借助脚本去实现.既 ...

  2. torchnet package (1)

    torchnet package (1) torchnet torchnet torchnet是用于torch的代码复用和模块化编程的框架,主要包含四个类 Dataset 以不同的方式对数据进行预处理 ...

  3. SVN 提交代码时强制加入注释内容

    参考: 关于SVN提交强制加入注释 方式一是使用的服务器脚本,对所有SVN用户生效. 方式二是使用本地钩子,仅对当前SVN用户生效.

  4. mysql 判断表字段是否存在,然后修改

    -- ---------------------------- -- 判断 vrv_paw_rule 表是否存在 thresholdMin 字段,不存在则添加; 存在则修改字段类型 DELIMITER ...

  5. SpringMvc测试框架详解----服务端测试

    随着RESTful Web Service的流行,测试对外的Service是否满足期望也变的必要的.从Spring 3.2开始Spring了Spring Web测试框架,如果版本低于3.2,请使用sp ...

  6. 联想A390T刷机ROOT教程

    一.联想A390T手动进入Recovery的方法: [步骤一]首先,将你的A390T手机关机,关机状态下,先按住电源键2秒,不要松开,再同时按下音量加.音量减两个键,此时,3个键一直按住不要放开,几秒 ...

  7. Kotlin Reference (九) Properties and Fields

    most from reference 声明属性 Koltin的类都有属性,这些属性可以声明为可变的,使用var关键字或用val关键字生声明不可变属性. class Address { var nam ...

  8. 如何从MTK机器的NVRAM中获取WIFI mac地址

    在MTK的机器中,如果不用特定的工具烧写MAC地址,在开机后打开WIFI后会显示: "NVRAM WARNING: Err=0x10" 这就是没有烧写mac地址的原因,所以每次打开 ...

  9. python print 控制台输出中文

    在pycharm里面的控制台用print输出信息,  本意想输出中文, 但是实际上是u\xxxx. 可以用这种方式: print("%s " % cn_string)

  10. hydra nodejs 微服务框架简单试用

    hydra 是一个以来redis 的nodejs 微服务框架 安装 需要redis,使用docker 进行运行 redis docker run -d -p 6379:6379 redis 安装yo ...