-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path622.py
150 lines (122 loc) · 3.84 KB
/
622.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
__________________________________________________________________________________________________
sample 68 ms submission
class MyCircularQueue:
def __init__(self, k: int):
"""
Initialize your data structure here. Set the size of the queue to be k.
"""
self.size = k
self.length = 0
self.queue = []
def enQueue(self, value: int) -> bool:
"""
Insert an element into the circular queue. Return true if the operation is successful.
"""
if self.length < self.size:
self.queue.append(value)
self.length += 1
return True
else:
return False
def deQueue(self) -> bool:
"""
Delete an element from the circular queue. Return true if the operation is successful.
"""
if self.isEmpty():
return False
self.queue.pop(0)
self.length -= 1
return True
def Front(self) -> int:
"""
Get the front item from the queue.
"""
if self.queue:
return self.queue[0]
else:
return -1
def Rear(self) -> int:
"""
Get the last item from the queue.
"""
if self.queue:
return self.queue[-1]
else:
return -1
def isEmpty(self) -> bool:
"""
Checks whether the circular queue is empty or not.
"""
return self.length == 0
def isFull(self) -> bool:
"""
Checks whether the circular queue is full or not.
"""
return self.length == self.size
# Your MyCircularQueue object will be instantiated and called as such:
# obj = MyCircularQueue(k)
# param_1 = obj.enQueue(value)
# param_2 = obj.deQueue()
# param_3 = obj.Front()
# param_4 = obj.Rear()
# param_5 = obj.isEmpty()
# param_6 = obj.isFull()
__________________________________________________________________________________________________
sample 13364 kb submission
class MyCircularQueue:
def __init__(self, k: int):
"""
Initialize your data structure here. Set the size of the queue to be k.
"""
self.queue = [None] * k
self.head = -1
self.tail = -1
self.size = k
def enQueue(self, value: int) -> bool:
"""
Insert an element into the circular queue. Return true if the operation is successful.
"""
if self.isFull():
return False
if self.head == -1:
self.head = 0
self.tail = (self.tail + 1) % self.size
self.queue[self.tail] = value
return True
def deQueue(self) -> bool:
"""
Delete an element from the circular queue. Return true if the operation is successful.
"""
if self.isEmpty():
return False
if self.head == self.tail:
self.head = -1
self.tail = -1
else:
self.head = (self.head + 1) % self.size
return True
def Front(self) -> int:
"""
Get the front item from the queue.
"""
if self.isEmpty():
return -1
return self.queue[self.head]
def Rear(self) -> int:
"""
Get the last item from the queue.
"""
if self.isEmpty():
return -1
return self.queue[self.tail]
def isEmpty(self) -> bool:
"""
Checks whether the circular queue is empty or not.
"""
return True if self.tail == -1 else False
def isFull(self) -> bool:
"""
Checks whether the circular queue is full or not.
"""
return True if (self.tail + 1) % self.size == self.head else False
__________________________________________________________________________________________________