首页 > 其他 > 详细

力扣232. 用栈实现队列

时间:2021-02-09 18:16:37      阅读:31      评论:0      收藏:0      [点我收藏+]

原题

 1 class MyQueue:
 2 
 3     def __init__(self):
 4         """
 5         Initialize your data structure here.
 6         """
 7         self.mainstack = []
 8         self.helpstack = []
 9 
10     def push(self, x: int) -> None:
11         """
12         Push element x to the back of queue.
13         """
14         self.mainstack += self.helpstack
15         self.mainstack.append(x)
16         self.helpstack = []
17 
18     def pop(self) -> int:
19         """
20         Removes the element from in front of queue and returns that element.
21         """
22         front = self.mainstack[0]
23         self.mainstack = self.mainstack[1:]
24         return front
25 
26     def peek(self) -> int:
27         """
28         Get the front element.
29         """
30         return self.mainstack[0]
31 
32     def empty(self) -> bool:
33         """
34         Returns whether the queue is empty.
35         """
36         return len(self.mainstack) < 1
37 
38 
39 # Your MyQueue object will be instantiated and called as such:
40 # obj = MyQueue()
41 # obj.push(x)
42 # param_2 = obj.pop()
43 # param_3 = obj.peek()
44 # param_4 = obj.empty()

 

力扣232. 用栈实现队列

原文:https://www.cnblogs.com/lj95/p/14393435.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!