leetcode225
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的更多相关文章
- [Swift]LeetCode225. 用队列实现栈 | Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- 【LeetCode225】 Implement Stack using Queues★
1.题目 2.思路 3.java代码 import java.util.LinkedList; import java.util.Queue; public class MyStack { priva ...
- Leetcode225 用栈实现队列
大众思路: 用两个栈实现,记为s1,s2 1.元素入栈时,加入s1 2.元素出栈时,对s2进行判断,如果s2为空,则将全部s1元素弹出并压入到s2,然后从s2栈顶弹出一个元素:如果s2不为空,则直接从 ...
- LeetCode225 Implement Stack using Queues
Implement the following operations of a stack using queues. (Easy) push(x) -- Push element x onto st ...
- LeetCode225 用队列实现栈
使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 注意: 你只能使用队列的基本操作 ...
- LeetCode-Stack-Easy
简单题 1. 有效的括号(leetcode-20) 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 1. 左括号必须用相同类型的右括 ...
- LeetCode通关:栈和队列六连,匹配问题有绝招
刷题路线参考: https://github.com/chefyuan/algorithm-base https://github.com/youngyangyang04/leetcode-maste ...
随机推荐
- Java实现ping功能的三种方法及Linux的区分
前大半部份转自:https://blog.csdn.net/futudeniaodan/article/details/52317650 检测设备的运行状态,有的是使用ping的方式来检测的.所以需要 ...
- java Cookie 获取历史记录列表(三)
/** * 获取Cookie里面的东西 */ protected List<String> getCookieList() { Cookie[] cookies = null; Cooki ...
- BOM之其他浏览器对象的使用
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- linux下部署tomcat服务器之安装jdk
如果一开始安装过jdk的可以卸载 rpm -qa | grep java rpm -e --nodeps java-1.6.0-openjdk-1.6.0.0-1.50.1.11.5.el6_3.x8 ...
- 转:XML 中的空白字符须知:xml:space
了解 XML 空白字符的概念并掌握如何避免与之相关的问题的技巧. 2006 年 4 月发布 很多时候,您可能都没注意到,在 XML 中所做的更改影响着您访问 XML 文档中数据的方式.例如: < ...
- react 拖拽排序---原生
定义css, 两个动画 .drag-up { -webkit-animation: dragup ease 0.2s 1; animation: dragup ease 0.2s 1; -webkit ...
- 阿里云windows时间同步服务地址
偶然发现的, 记录一下 ntp1.aliyun.com
- Ubuntu系统安装,适用于14.04,16.04和17.10
本文以14.04为案例进行安装,其他版本相关问题会做注解 1.选择要安装的系统语言 本界面建议选择English,之后再选择中文安装 注意: 安装服务器版时,对于14.x版本第一步选择中文没有问题,但 ...
- rancher下的kubernetes之二:安装rancher和kubernetes
在上一章<rancher下的kubernetes之一:构建标准化vmware镜像>,我们做了个通用的虚拟机镜像,可以root登录,apt已经更新,docker也装好了,现在我们就来安装ra ...
- Jmeter-Critical Section Controller(临界区控制器)
The Critical Section Controller ensures that its children elements (samplers/controllers, etc.) will ...