问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4108 访问。

使用栈实现队列的下列操作:

push(x) -- 将一个元素放入队列的尾部。

pop() -- 从队列首部移除元素。

peek() -- 返回队列首部的元素。

empty() -- 返回队列是否为空。

MyQueue queue = new MyQueue();

queue.push(1);

queue.push(2);

queue.peek();  // 返回 1

queue.pop();   // 返回 1

queue.empty(); // 返回 false

说明:

你只能使用标准的栈操作 -- 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。

你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。

假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。


Implement the following operations of a queue using stacks.

push(x) -- Push element x to the back of queue.

pop() -- Removes the element from in front of queue.

peek() -- Get the front element.

empty() -- Return whether the queue is empty.

MyQueue queue = new MyQueue();

queue.push(1);

queue.push(2);

queue.peek();  // returns 1

queue.pop();   // returns 1

queue.empty(); // returns false

Notes:

You must use only standard operations of a stack -- which means only push to top, peek/pop from top, size, and is empty operations are valid.

Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.

You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4108 访问。

public class Program {

    public static void Main(string[] args) {
var queue = new MyQueue(); queue.Push(1);
queue.Push(2);
queue.Push(3); Console.WriteLine(queue.Peek());
Console.WriteLine(queue.Pop());
Console.WriteLine(queue.Empty()); Console.ReadKey();
} public class MyQueue { private Stack<int> _stack = null; public MyQueue() {
_stack = new Stack<int>();
} public void Push(int x) {
//基本思路是反转原栈
var stack = new Stack<int>();
stack.Push(x);
var reverse = _stack.Reverse().ToList();
foreach(var elemet in reverse) {
stack.Push(elemet);
}
_stack = stack;
} public int Pop() {
return _stack.Pop();
} public int Peek() {
return _stack.Peek();
} public bool Empty() {
return _stack.Count == 0;
} } }

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4108 访问。

1
1
False

分析:

显而易见,因为部分运行库的使用,Push 的时间复杂度应当为:  ,其它方法的时间复杂度应当为:  。

C#LeetCode刷题之#232-用栈实现队列​​​​​​​​​​​​​​(Implement Queue using Stacks)的更多相关文章

  1. LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4

    232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...

  2. LeetCode 232:用栈实现队列 Implement Queue using Stacks

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

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

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

  4. LeetCode 刷题笔记 155. 最小栈(Min Stack)

    tag: 栈(stack) 题目描述 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素 ...

  5. C#LeetCode刷题之#155-最小栈(Min Stack)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4020 访问. 设计一个支持 push,pop,top 操作,并能 ...

  6. C#LeetCode刷题-栈

    栈篇 # 题名 刷题 通过率 难度 20 有效的括号 C#LeetCode刷题之#20-有效的括号(Valid Parentheses) 33.0% 简单 42 接雨水   35.6% 困难 71 简 ...

  7. C#LeetCode刷题-设计

    设计篇 # 题名 刷题 通过率 难度 146 LRU缓存机制   33.1% 困难 155 最小栈 C#LeetCode刷题之#155-最小栈(Min Stack) 44.9% 简单 173 二叉搜索 ...

  8. LeetCode刷题 --杂篇 --数组,链表,栈,队列

    武汉加油,中国加油.希望疫情早日结束. 由于疫情,二狗寒假在家不能到处乱逛,索性就在家里系统的刷一下算法的内容,一段时间下来倒也有些小小的收获.只是一来家中的小破笔记本写起博客来实在不是很顺手,二来家 ...

  9. leetcode刷题记录——栈和队列

    题目 232.用栈实现队列 class MyQueue { private Stack<Integer> in = new Stack<>(); private Stack&l ...

  10. LeetCode刷题专栏第一篇--思维导图&时间安排

    昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...

随机推荐

  1. Appium+Python3环境搭建,其实超简单!【软件测试教程】

    appium可以说是做app最火的一个自动化框架,它的主要优势是支持android和ios,另外脚本语言也是支持java和Python.略懂Python,所以接下来的教程是appium+python, ...

  2. CUDA C++ Extensions

    敲代码的时候总是会去CUDA官方文档中找找思路,感觉每次看英文文档都要耗费一点时间来翻译,干脆自己翻译一下便于以后查阅.官方文档:cuda-c-language-extensions 目录 函数修饰符 ...

  3. 给咱的服务器安装BBR脚本

    yum -y install wget ##ContOS Yum 安装 wget apt-get install wget ##Debian Ubuntu 安装 wget 先给咱的服务器安装wget, ...

  4. git push到远程新分支

    获取远程代码并在本地切换到一个新分支修改后,想要 push 到远端与原来不同的新分支,可以使用下面的命令实现: git push origin 本地分支:远端希望创建的分支 上面的本地分支 是基于拉取 ...

  5. websphere8.5配置db2数据源

    websphere8.5配置db2数据源 1. 打开websphere控制台  2.进入websphere变量页面  3. 进入DB2UNIVERSAL_JDBC_DRIVER_NATIVEPATH变 ...

  6. spring读取jdbc(file方式)

    使用PropertyPlaceholderConfigurer类载入外部配置 在Spring项目中,你可能需要从properties文件中读入配置注入到bean中,例如数据库连接信息,memcache ...

  7. 浅析MySQL中change与modify的区别

    MySQL版本 show variables like 'version'; 表结构 desc student; 修改表 例如:修改表student的name字段,将varchar(10)修改为var ...

  8. Nginx配置各种响应头防止XSS,点击劫持,frame恶意攻击

    为什么要配置HTTP响应头? 不知道各位有没有被各类XSS攻击.点击劫持 (ClickJacking. frame 恶意引用等等方式骚扰过,百度联盟被封就有这些攻击的功劳在里面.为此一直都在搜寻相关防 ...

  9. VSCode package.json warning: Problems loading reference 'https://json.schemastore.org/package'...

    报错内容 Problems loading reference 'https://json.schemastore.org/package': Unable to load schema from ' ...

  10. Day15_redis安装及配置

    学于黑马和传智播客联合做的教学项目 感谢 黑马官网 传智播客官网 微信搜索"艺术行者",关注并回复关键词"乐优商城"获取视频和教程资料! b站在线视频 redi ...