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. Android Studio之高德地图实现定位和3D地图显示

    在应用开发中,地图开发是经常需要使用的“组件”,国内比较出名的是就是百度地图和高德地图. 此博客讲的是高德地图实现定位和3D地图显示,并标注相应位置,话不多说,先看看效果,在上代码. 效果如图: 首先 ...

  2. bzoj2120

    题解: 可修改莫队 我们加入一个时间T 然后在排序的时候考虑一下时间 在计算的时候也要考虑 代码: #include<bits/stdc++.h> using namespace std; ...

  3. SQL 字符串拆分

    字符串拆分: ALTER FUNCTION [dbo].[f_Split](@sText nvarchar(max),@split NVARCHAR(20)) RETURNS @t TABLE (id ...

  4. 6.4-6.5 使用form表单验证,完善登录页面

    之前是使用自定义的类来实现登录逻辑,现在使用django内置的form表单验证,用继承django的view来实现登录页面. users > views.py 的内容是: from django ...

  5. c/c++ socket函数详解

    c/c++ socket函数详解 注意: 使用socketAPI前,要先将相关链接库(Ws2_32.lib)加入链接,并使用WSAStartUp函数初始化.每个socket函数都可能失败(返回-1), ...

  6. .NET 命令行参数包含应用程序路径吗?

    如果你关注过命令行参数,也许发现有时你会在命令行参数的第一个参数中中看到应用程序的路径,有时又不会.那么什么情况下有路径呢? 其实是否有路径只是取决于获取命令行参数的时候用的是什么方法.而这是 Win ...

  7. 在Linux中批量修改字符串的命令

    昨天一个朋友忽然问我,在Linux下如何批量修改字符串,当时瞬间懵逼了,完全想不起来....... 今天特意的重温了一下Linux下的一些常用命令,并将这个遗忘的批量修改字符串的命令记录下来(资料来自 ...

  8. IDEA java开发学习笔记

    JDK8下载地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 配置环境变量 ...

  9. jenkins需要安装的插件

    jenkins plugin 需要安装的插件: •发布插件 Deploy to container Plugin 必须 •Maven插件 Maven Integration plugin必须 •git ...

  10. pthread访问调用信号线程的掩码(pthread_sigmask )

    掩码: 信号掩码 在POSIX下,每个进程有一个信号掩码(signal mask).简单地说,信号掩码是一个"位图",其中每一位都对应着一种信号.如果位图中的某一位为1,就表示在执 ...