1.编辑器

我使用的是win10+vscode+leetcode+python3
环境配置参见我的博客:
链接

2.第二百三十二题

(1)题目
英文:
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.
Example:

MyQueue queue = new MyQueue();

queue.push(1);
queue.push(2);
queue.peek(); // returns 1
queue.pop(); // returns 1
queue.empty(); // returns false

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

push(x) – 将一个元素放入队列的尾部。
pop() – 从队列首部移除元素。
peek() – 返回队列首部的元素。
empty() – 返回队列是否为空。
示例:

MyQueue queue = new MyQueue();

queue.push(1);
queue.push(2);
queue.peek(); // 返回 1
queue.pop(); // 返回 1
queue.empty(); // 返回 false

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/implement-queue-using-stacks

(2)解法
全部用库
(耗时:32ms,内存:13.5M)

class MyQueue:def __init__(self):"""Initialize your data structure here."""self.queue=[]def push(self, x: int) -> None:"""Push element x to the back of queue."""self.queue.append(x)def pop(self) -> int:"""Removes the element from in front of queue and returns that element."""return self.queue.pop(0)def peek(self) -> int:"""Get the front element."""return self.queue[0]def empty(self) -> bool:"""Returns whether the queue is empty."""return not bool(self.queue)# Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()

注意:
1.deque中的pop是不能有索引的,必须是pop最后边的元素;而list中的pop是可以选择索引的。

② 原始方法
(耗时:44ms,内存:13.5M)

class MyQueue:def __init__(self):"""Initialize your data structure here."""self.queue=[]def push(self, x: int) -> None:"""Push element x to the back of queue."""self.queue.append(x)def pop(self) -> int:"""Removes the element from in front of queue and returns that element."""buff=[]while len(self.queue)>0:buff.append(self.queue[-1])del self.queue[-1]num=buff[-1]del buff[-1]while len(buff)>0:self.queue.append(buff[-1])del buff[-1]return numdef peek(self) -> int:"""Get the front element."""buff=[]length=len(self.queue)while length>0:buff.append(self.queue[length-1])length-=1return buff[-1]def empty(self) -> bool:"""Returns whether the queue is empty."""return True if len(self.queue)==0 else False# Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()

leetcode python3 简单题232. Implement Queue using Stacks相关推荐

  1. leetcode python3 简单题225. Implement Stack using Queues

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百二十五题 (1)题目 英文: Implement the following ...

  2. leetcode python3 简单题28. Implement strStr()

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二十八题 (1)题目 英文: Implement strStr(). Return ...

  3. LeetCode刷题记录12——232. Implement Queue using Stacks(easy)

    LeetCode刷题记录12--232. Implement Queue using Stacks(easy) 目录 LeetCode刷题记录12--232. Implement Queue usin ...

  4. leetcode python3 简单题69. Sqrt(x)

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第六十九题 (1)题目 英文: Implement int sqrt(int x). ...

  5. leetcode python3 简单题136. Single Number

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百三十六题 (1)题目 英文: Given a non-empty array ...

  6. leetcode python3 简单题111. Minimum Depth of Binary Tree

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十一题 (1)题目 英文: Given a binary tree, fin ...

  7. leetcode python3 简单题104. Maximum Depth of Binary Tree

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百零四题 (1)题目 英文: Given a binary tree, find ...

  8. leetcode python3 简单题70. Climbing Stairs

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第七十题 (1)题目 英文: You are climbing a stair ca ...

  9. leetcode python3 简单题53. Maximum Subarray

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第五十三题 (1)题目 英文: Given an integer array num ...

最新文章

  1. 简述安装Tsurugi Linux发行版
  2. 零基础代码学python-零基础学Python 超详细教程
  3. jquery和JavaScript区别
  4. 离线轻量级大数据平台Spark之MLib机器学习库聚类算法KMeans实例
  5. para1、para2与**kw
  6. Windows Azure Storage论文解读
  7. 汇编语言(三十五)之输入字符串以$结束然后输出字母个数
  8. 0与1世界的初级编程篇之C语言
  9. 视频AI,助力体育赛事转播走进智能时代
  10. haproxy+keepalived实现高可用K8S集群部署
  11. win10电脑突然开不了热点,怎么办
  12. 17秋 软件工程 团队第三次作业 预则立他山之石
  13. centos locate搜索工具
  14. [计算机网络] P2P 协议
  15. 慕课版软件质量保证与测试(第四章.课后作业)
  16. 使用客户端工具SecureCRT连接linux
  17. epoll文件服务器,使用epoll模型的服务器
  18. 微信订阅出现errMsg“:“requestSubscribeMessage:fail can only be invoked by user TAP gesture 解决方案
  19. 清新小学生文明礼仪PPT模板
  20. bootstrap-table固定表头固定列

热门文章

  1. c性能大容量cket_5千左右预算,既轻薄(高颜值)又高性能的笔记本推荐(畅玩LOL、CF、DNF、流放之路、梦幻西游)...
  2. cdh中hue集成hbase_HBase版本 | Cloudera Enterprise 6.2.0发布
  3. Evolution Game DP
  4. 【机器学习-西瓜书】八、集成学习:结合策略;多样性;总结
  5. 二维前缀和(附模板题)
  6. 一维前缀和(附模板题)
  7. C++中的getline函数
  8. MATLAB中的命令行输出
  9. Android编程中利用AudioTrack播放PCM数据在音频的最后出现重复回声现象的解决方案
  10. 东大OJ-1588: Routing Table