leetcode232
public class MyQueue
{
Stack<int> S = new Stack<int>();
/** Initialize your data structure here. */
public MyQueue()
{ } /** Push element x to the back of queue. */
public void Push(int x)
{
S.Push(x);
} /** Removes the element from in front of queue and returns that element. */
public int Pop()
{
List<int> list = new List<int>();
var x = -;
while (S.Count > )
{
x = S.Pop();
list.Add(x);
} for (int i = list.Count - ; i >= ; i--)
{
S.Push(list[i]);
} return x;
} /** Get the front element. */
public int Peek()
{
if (S.Count > )
{
return S.ElementAt(S.Count - );
}
else
{
return -;
}
} /** Returns whether the queue is empty. */
public bool Empty()
{
return S.Count == ;
}
} /**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.Push(x);
* int param_2 = obj.Pop();
* int param_3 = obj.Peek();
* bool param_4 = obj.Empty();
*/
https://leetcode.com/problems/implement-queue-using-stacks/#/description
leetcode232的更多相关文章
- [Swift]LeetCode232. 用栈实现队列 | Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- 【LeetCode232】 Implement Queue using Stacks★
1.题目描述 2.思路 思路简单,这里用一个图来举例说明: 3.java代码 public class MyQueue { Stack<Integer> stack1=new Stack& ...
- LeetCode232:Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) – Push element x to the back of ...
- LeetCode232 Implement Queue using Stacks Java 题解
题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the bac ...
- LeetCode232 用栈实现队列
使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列首部的元素. empty() -- 返回队列是否为空. ...
- 面试之leetcode20堆栈-字符串括号匹配,队列实现栈
1 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合.左括号必须以正确的顺序闭合.注意空字符串可被认 ...
- LeetCode-Stack-Easy
简单题 1. 有效的括号(leetcode-20) 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 1. 左括号必须用相同类型的右括 ...
- LeetCode通关:栈和队列六连,匹配问题有绝招
刷题路线参考: https://github.com/chefyuan/algorithm-base https://github.com/youngyangyang04/leetcode-maste ...
随机推荐
- 001PHP文件处理——文件处理disk_total_space disk_free_space basename dirname file_exists filetype
<?php /** * 文件处理disk_total_space disk_free_space basename dirname file_exists filetype */ //disk_ ...
- RabbitMQ三种Exchange模式
RabbitMQ中,所有生产者提交的消息都由Exchange来接受,然后Exchange按照特定的策略转发到Queue进行存储 RabbitMQ提供了四种Exchange:fanout,direct, ...
- Nginx实践03-配置虚拟主机的3种方式
基于IP.端口号.名称3种方式 1.基于IP的虚拟主机配置(使用最少) 基于ip的虚拟主机配置,需要配置单个网卡上多个ip地址,这种方式管理比较麻烦,所以用的很少. 1.1 设置单个网卡多个IP 查看 ...
- zTree简单使用
zTree使用 zTree github地址 zTree API文档 zTree插件依赖JQ所以使用zTree首先引入JQ,另外zTree的点击功能,编辑功能都是单独的文件,如需使用也要引入(也可以引 ...
- (转)高并发Web服务的演变——节约系统内存和CPU
一.越来越多的并发连接数 现在的Web系统面对的并发连接数在近几年呈现指数增长,高并发成为了一种常态,给Web系统带来不小的挑战.以最简单粗暴的方式解决,就是增加Web系统的机器和升级硬件配置.虽然现 ...
- 1029:Ignatius and the Princess IV
题目大意是找出数组中出现次数超过一半的数. 基本思想:每遇到两个不同的数就消掉,设一个计数器就行了. 存出现次数最大的那个数的出现次数. 当下一个数与当前的数不同时,计数器减一,相同,则加一. 实 ...
- 《DSP using MATLAB》示例 Example 9.11
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- ACCESS_TOKEN与FRESH_TOKEN
OAuth1.0中的access_token过期时间通常很长,安全性差.于是OAuth2.0推出了refresh_token. OAuth2.0中,客户端用账户名,密码经过一定方式(比如先请求code ...
- Python菜鸟之路:Django 路由、模板、Model(ORM)
Django路由系统 Django的路由系统让Django可以根据URI进行匹配,进而发送至特定的函数去处理用户请求.有点类似nginx的location功能. Django的路由关系分为三种:普通关 ...
- Mysql5.6 buffer_pool预热功能
通常在mysql重启服务后,需要通过手工执行SQL来预热buffer_pool,在mysql5.6中,有如下参数可以无需人工干预. innodb_buffer_pool_dump_at_shutdow ...