用两个栈实现队列

news/2024/7/7 22:42:23

用两个栈实现队列

【题目】:

用两个栈实现一个队列,实现这个队列的删除头部deleteHead和插入尾部appendTail的功能。

示例 1:

输入:
["CQueue","appendTail","deleteHead","deleteHead"]
[[],[3],[],[]]
输出:[null,null,3,-1]

示例 2:

输入:
["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
[[],[],[5],[2],[],[]]
输出:[null,-1,null,null,5,2]

【解题思路】:

  • 构造两个栈:stack1, stack2:
  • 从队列中删除一个头节点: 先将数据压入到stack1中, 要删除头节点的时候,将stack1中的元素出栈再压入到stack2中,这时stack2栈顶的元素就是头节点;
  • 插入一个为尾节点:直接将数据压入到stack1中。
     
class MyQueue(object):def __init__(self):self.stack1 = []self.stack2 = []def append_tail(self, val):self.stack1.append(val)print("The %d is added to tail of the queue" % val)def delete_head(self):if self.stack2:print("The head is deleted from the queue, the head value is:", self.stack2.pop(-1))else:while self.stack1:self.stack2.append(self.stack1.pop(-1))if not self.stack2:print("The queue is empty!")returnprint("The head is deleted from the queue, the head value is:", self.stack2.pop(-1))# test
q = MyQueue()
# delete head from an empty queue
print("# delete head from an empty queue")
q.delete_head()
# add [1,2,3] to the queue, and then delete the head
print("# add [1,2,3] to the queue, and then delete the head")
for i in [1, 2, 3]:q.append_tail(i)
q.delete_head()# add [4, 5] to the queue, and the delete the head twice
print("# add [4, 5] to the queue, and the delete the head twice")
for i in [4, 5]:q.append_tail(i)
for i in range(2):q.delete_head()# delete the head 3 times
print("# delete the head 3 times")
for i in range(3):q.delete_head()

运行结果:

示例代码2:

class CQueue(object):def __init__(self):self.stack1 = []self.stack2 = []def appendTail(self, value):""":type value: int:rtype: None"""self.stack1.append(value)def deleteHead(self):""":rtype: int"""if self.stack2:return self.stack2.pop()if not self.stack1:return -1while self.stack1:self.stack2.append(self.stack1.pop())return self.stack2.pop()# Your CQueue object will be instantiated and called as such:
# obj = CQueue()
# obj.appendTail(value)
# param_2 = obj.deleteHead()

 


http://lihuaxi.xjx100.cn/news/266750.html

相关文章

Java 8 中的方法引用,轻松减少代码量,提升可读性!

点击上方蓝色“方志朋”,选择“设为星标”回复“666”获取独家整理的学习资料!1. 引言Java8中最受广大开发中喜欢的变化之一是因为引入了 lambda 表达式,因为这些表达式允许我们放弃匿名类,从而大大减少了样板代码,并提…

干货 | 一文完全理解AUC-ROC曲线

点击上方“小白学视觉”,选择加"星标"或“置顶”重磅干货,第一时间送达来源: https://towardsdatascience.com/understanding-auc-roc-curve-68b2303cc9c5 翻译:石头 机器学习模型的性能测量是一项必不可少的工作…

Io流的字节流与缓冲流

当我们队大量数据进行保存时可以用数组,当数据到达一定量时或给用户一个易懂得接口时就可采用IO流: IO流按进行的操作分输出流与输入流InputStream与OutputSteam 按操作的原理来分有2种常见的IO流字节流与缓冲流:这2种IO的的输入输出流都是对…

struts2请求处理过程源代码分析(1)

2019独角兽企业重金招聘Python工程师标准>>> 转载自:http://www.see-source.com/ 源码解析网 网上对于struts2请求处理流程的讲解还是比较多的,有的还是非常详细的,所以这里我就简单地将大概流程总结下,有了个大概印象…

python错误提示:TypeError: ‘builtin_function_or_method‘ object is not subscriptable

[] 换成 () >>> yy.replace[a,s] Traceback (most recent call last):File "<stdin>", line 1, in <module> TypeError: builtin_function_or_method object is not subscriptable >>> yy.replace(a,s) sbcdef >>>

22.加密与安全相关,证书申请CA(gpg,openssl)

安全机制 信息安全防护的目标保密性 Confidentiality完整性 Integrity可用性 Usability可控制性 Controlability不可否认性 Non-repudiation安全防护环节物理安全&#xff1a;各种设备/主机、机房环境系统安全&#xff1a;主机或设备的操作系统应用安全&#xff1a;各种网络服务…

用Python玩转PPT!

作者 | 陈熹来源 | 早起Python今天本文将基于第三方库pptx&#xff0c;详细讲解如何使用Python操作Office全家桶最后一位——PPT。安装pptx是一个非标准库&#xff0c;需要在命令行中安装pip install python-pptx要注意&#xff0c;安装的时候是python-pptx&#xff0c;而实际调…

求你了,不要再在对外接口中使用枚举类型了!

点击上方蓝色“方志朋”&#xff0c;选择“设为星标”回复“666”获取独家整理的学习资料&#xff01;最近&#xff0c;我们的线上环境出现了一个问题&#xff0c;线上代码在执行过程中抛出了一个IllegalArgumentException&#xff0c;分析堆栈后&#xff0c;发现最根本的的异常…