-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1670.设计前中后队列.py
75 lines (57 loc) · 1.36 KB
/
1670.设计前中后队列.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#
# @lc app=leetcode.cn id=1670 lang=python
#
# [1670] 设计前中后队列
#
# @lc code=start
class FrontMiddleBackQueue(object):
def __init__(self):
self.stack = []
def pushFront(self, val):
"""
:type val: int
:rtype: None
"""
self.stack.insert(0, val)
def pushMiddle(self, val):
"""
:type val: int
:rtype: None
"""
self.stack.insert(len(self.stack)// 2, val)
def pushBack(self, val):
"""
:type val: int
:rtype: None
"""
self.stack.append(val)
def popFront(self):
"""
:rtype: int
"""
if len(self.stack) == 0:
return -1
return self.stack.pop(0)
def popMiddle(self):
"""
:rtype: int
"""
if len(self.stack) == 0:
return -1
return self.stack.pop(len(self.stack)// 2 - (len(self.stack) % 2 == 0))
def popBack(self):
"""
:rtype: int
"""
if len(self.stack) == 0:
return -1
return self.stack.pop(-1)
# Your FrontMiddleBackQueue object will be instantiated and called as such:
# obj = FrontMiddleBackQueue()
# obj.pushFront(val)
# obj.pushMiddle(val)
# obj.pushBack(val)
# param_4 = obj.popFront()
# param_5 = obj.popMiddle()
# param_6 = obj.popBack()
# @lc code=end