问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)

    目录 题目描述: 示例: 说明: 解法: 题目描述: 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列 ...

  2. 使用栈实现队列 Implement Queue using Stacks

    为什么80%的码农都做不了架构师?>>>    问题: Implement the following operations of a queue using stacks. pus ...

  3. [LeetCode]-Python刷题第三周(栈和队列)

    20. Valid Parentheses 合法括号(Easy) Given a string containing just the characters '(', ')', '{', '}', ' ...

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

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

  5. 【leetcode刷题笔记】单调栈

    典型例题:求左边第一个比当前元素小的数 (模板,弄懂) 代码: #include<iostream> using namespace std; #include<stack> ...

  6. leetcode算法题--用两个栈实现队列

    原题链接:https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/ class CQueue {public ...

  7. C#LeetCode刷题-栈

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

  8. C#LeetCode刷题-设计

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

  9. ​LeetCode刷题实战623:在二叉树中增加一行

    算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...

  10. LeetCode刷题C++实录

    LeetCode刷题C++实录 1. 两数之和 121. 买卖股票的最佳时机 382. 链表随机节点 622. 设计循环队列 623. 在二叉树中增加一行 640. 求解方程 761. 特殊的二进制序 ...

最新文章

  1. 201671010128 2017-11-12《Java程序设计》之图形程序设计
  2. Typescript学习笔记(二)
  3. PAT甲级1059 Prime Factors :[C++题解]分解质因子
  4. 西安科技大学计算机考研难度,西安科技大学考研难吗
  5. python mysql写入速度加快_解决python写入mysql中datetime类型遇到的问题
  6. mysql 两张大表关联_MySQL的DropTable影响分析和最佳实践
  7. 低价iPhone 12彻底没戏了?苹果严控渠道:给拼多多等电商供货罚款40万元/台
  8. windows android ios,TIM for Windows/iOS/Android v2.2.6 正式版发布
  9. ❤️《集成SSM框架—图书系统》Mybatis+Spring+SpirngMVC
  10. Webx MVC分析
  11. 花了很长时间看完了 java编程思想
  12. 在Ubuntu系统上安装minidwep-gtk软件
  13. word交叉引用自动编排word参考文献
  14. 帮助你提高排版技巧的18个 PS 文字特效教程
  15. 小米10性能再进化!小米带来国内安卓系统GPU驱动首次更新——初阶GPU及其驱动升级认识
  16. twitter推文采集案例
  17. GitHub上重要的几个搜索技巧
  18. [hdu 6355] Fireflies
  19. 江苏省事业单位计算机入面分数线,江苏事业单位进面分数线一览!进面名单有你吗...
  20. matlab 调用mex,从MATLAB中调用MEX中的C类方法

热门文章

  1. mysql 用户授权
  2. 【今日CS 视觉论文速览】Fri, 21 Dec 2018
  3. python中的编码问题
  4. Java—多线程创建详解
  5. Scanner对象接收数据进行分类处理 java
  6. 爬虫-request库-get请求
  7. 爬虫-xpath的用法强化
  8. python-函数的嵌套调用
  9. python-列表包字典-字典结构格式化-经验
  10. 禁止用户通过shell登录