问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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 的时间复杂度应当为:  ,其它方法的时间复杂度应当为:  。

最新文章

  1. C#开发微信门户及应用(30)--消息的群发处理和预览功能
  2. Screenfly – 各种设备的屏幕和分辨率下快速测试网站
  3. android 中Activity的onStart()和onResume()的区别是什么
  4. OpenCV 2.2版本号以上显示图片到 MFC 的 Picture Control 控件中
  5. hdu 5585 Numbers【大数+同余定理】
  6. HDOJ/HDU Tempter of the Bone(深搜+奇偶性剪枝)
  7. poj2352 Stars
  8. MC- 交易并设置止损
  9. 开涛spring3(8.1) - 对ORM的支持 之 8.1 概述
  10. iOS开发实战-上架AppStore 通过内购和广告获得收益
  11. jquery源码分析之一前言篇
  12. 2016-wing的年度总结
  13. HOW TO LINK THE TRANSACTION_SOURCE_ID TO TRANSACTION_SOURCE_TYPE_ID
  14. wordpress背景添加跟随鼠标动态线条特效
  15. Django 模板 继承和包含
  16. jQuery-lazyload参数
  17. Codeforces Round #406 (Div. 1) A. Berzerk 记忆化搜索
  18. Android入门学习总结
  19. js 刷新页面
  20. Spring4源码解析:BeanDefinition架构及实现

热门文章

  1. Qt_IO系统_二进制读写
  2. 信不信?各种红包App最后都会整合游戏!App+游戏的变现模式分析
  3. IDEA添加注释常用的快捷键
  4. 什么是CSV
  5. python基础--小数据池,代码块的最详细、深入剖析
  6. Zookeeper集群部署及报错分析
  7. java 控制语句、数组、方法
  8. 微服务迁移记(五):WEB层搭建(1)
  9. 第二部分_Mac技巧
  10. Python List min()方法