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的更多相关文章

  1. [Swift]LeetCode232. 用栈实现队列 | Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  2. 【LeetCode232】 Implement Queue using Stacks★

    1.题目描述 2.思路 思路简单,这里用一个图来举例说明: 3.java代码 public class MyQueue { Stack<Integer> stack1=new Stack& ...

  3. LeetCode232:Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) – Push element x to the back of ...

  4. LeetCode232 Implement Queue using Stacks Java 题解

    题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the bac ...

  5. LeetCode232 用栈实现队列

    使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列首部的元素. empty() -- 返回队列是否为空. ...

  6. 面试之leetcode20堆栈-字符串括号匹配,队列实现栈

    1 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合.左括号必须以正确的顺序闭合.注意空字符串可被认 ...

  7. LeetCode-Stack-Easy

    简单题 1. 有效的括号(leetcode-20) 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 1. 左括号必须用相同类型的右括 ...

  8. LeetCode通关:栈和队列六连,匹配问题有绝招

    刷题路线参考: https://github.com/chefyuan/algorithm-base https://github.com/youngyangyang04/leetcode-maste ...

随机推荐

  1. ActiveRecord 惰性加载,和使用gem faker

    rails console后: 2.1.4 :001 > User # => User (call 'User.connection' to establish a connection) ...

  2. 009PHP文件处理——文件处理 file_get_contents file_put_contents fgetc fgets fgetss

    <?php /** * 文件处理 file_get_contents file_put_contents fgetc fgets fgetss */ //fgetc() 传入文件操作句柄.每次获 ...

  3. do-while语句和while的区别

    do-while语句是一种后测试循环语句,即只有在循环体中的代码执行之后,才会测试出口条件.其实就是,代码在刚开始执行的时候,都是要先走一遍do循环体内的代码,然后在与while里面的条件进行判断,成 ...

  4. sgu 142. Keyword 暴力,hash 难度:0

    142. Keyword time limit per test: 0.5 sec. memory limit per test: 16384 KB Kevin has invented a new ...

  5. css单位长度

    CSS长度单位 单位 含义 em 相对于父元素的字体大小 ex 相对于小写字母”x”的高度 gd 一般用在东亚字体排版上,这个与英文并无关系 rem 相对于根元素字体大小 vw 相对于视窗的宽度:视窗 ...

  6. 对象存储在什么地方(java编程思想)

    用引用操作对象.创建了一个引用,需要进行初始化(与事物进行关联),才能正常使用.new将引用于对象进行关联 对象存储到什么地方? 程序运行时,对象是怎么进行放置安排的呢?特别是内存是怎么分配的呢?对这 ...

  7. Leetcode 1022. Sum of Root To Leaf Binary Numbers

    dfs class Solution: def sumRootToLeaf(self, root: TreeNode) -> int: stack=[(root,0)] ans=[] bi_st ...

  8. DataReader使用

    一.DataReader含义 DataReader相比于DataSet,DataReader是一个抽象类,所以不能用DataReader DR = new DataReader(),来构造函数创建对象 ...

  9. Ubuntu用户管理

    本文主要介绍Ubuntu的用户管理,包括建立和删除用户,用户授权等 ================== 创建用户并授权================== sudo adduser xxx 会在ho ...

  10. Linux 环境下 javac 编译错误: 编码UTF8的不可映射字符 (编码UTF8/GBK的不可映射字符)

    Linux 系统下一般默认使用UTF-8编码, 使用javac 编辑使用其他编码格式编写的源吗时,会出现  “ 错误: 编码UTF8的不可映射字符 ”. 最近在使用  javac 编译 一个在wind ...