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 ...
随机推荐
- ActiveStorage. 英文书Learnrails5.2的案例,看如何放到云上。
ActiveStorage. 英文书Learnrails5.2的案例 本例子目标:增加一个avatar image给每个用户. 准备: 需要安装Imagemagick software.它可以crea ...
- hdu4280网络流之dinic
这题就是个模板题,不过我是第一次写dinic,好久没用链式前向星又不会了... 时间:9126ms #include<map> #include<set> #include&l ...
- python学习笔记(一)---python下载以及环境的安装
转载网址:https://www.runoob.com/python/python-install.html 1.下载python安装包: 安装包下载网址(如下图所在的网址):https://www. ...
- vue.js 源代码学习笔记 ----- text-parse.js
/* @flow */ import { cached } from 'shared/util' import { parseFilters } from './filter-parser' //找到 ...
- Linux DMA Engine framework(2)_功能介绍及解接口分析
http://www.wowotech.net/linux_kenrel/dma_engine_api.html 补充 http://www.zhimengzhe.com/linux/259646.h ...
- RF/GBDT/XGBoost/LightGBM简单总结(完结)
这四种都是非常流行的集成学习(Ensemble Learning)方式,在本文简单总结一下它们的原理和使用方法. Random Forest(随机森林): 随机森林属于Bagging,也就是有放回抽样 ...
- tf.device()指定tensorflow运行的GPU或CPU设备
在tensorflow中,我们可以使用 tf.device() 指定模型运行的具体设备,可以指定运行在GPU还是CUP上,以及哪块GPU上. 设置使用GPU 使用 tf.device('/gpu:1' ...
- php 递归读取目录
看到很多面试题有这个,今天有机会写了一下. 要注意的是: 在opendir这个函数用完后,要注意closedir,因为安全问题,打开的目录依然存在于内存中,在并发情况下最好关闭,不然容易被破坏. &l ...
- 用 dotTrace 进行性能分析时,各种不同性能分析选项的含义和用途
对 .NET 程序进行性能分析,dotTrace 能应对绝大多数的场景.在开启一个进程进行性能分析之前,我们会看到一些性能分析选项(Profiler Options).本文将介绍这几个选项的含义,并用 ...
- .NET/C# 在代码中测量代码执行耗时的建议(比较系统性能计数器和系统时间)
我们有很多种方法评估一个方法的执行耗时,比如使用性能分析工具,使用基准性能测试.不过传统的在代码中编写计时的方式依然有效,因为它可以生产环境或用户端得到真实环境下的执行耗时. 如果你希望在 .NET/ ...