-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path622.design-circular-queue.py
136 lines (107 loc) · 3.19 KB
/
622.design-circular-queue.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
#
# @lc app=leetcode id=622 lang=python3
#
# [622] Design Circular Queue
#
# @lc code=start
# TAGS: Design, Queue
class MyCircularQueue:
""" 68 ms, 80.47% """
def __init__(self, k: int):
"""
Initialize your data structure here. Set the size of the queue to be k.
"""
self.data = [None] * k
self.head = self.tail = self.len = 0
def enQueue(self, value: int) -> bool:
"""
Insert an element into the circular queue. Return true if the operation is successful.
"""
if self.data[self.head] is None:
self.data[self.head] = value
self.head = (self.head + 1) % len(self.data)
self.len += 1
return True
return False
def deQueue(self) -> bool:
"""
Delete an element from the circular queue. Return true if the operation is successful.
"""
if self.len:
self.data[self.tail] = None
self.tail = (self.tail + 1) % len(self.data)
self.len -= 1
return True
return False
def Front(self) -> int:
"""
Get the front item from the queue.
"""
if self.len:
return self.data[self.tail]
return -1
def Rear(self) -> int:
"""
Get the last item from the queue.
"""
if self.len:
temp = (self.head - 1) % len(self.data)
return self.data[temp]
return -1
def isEmpty(self) -> bool:
"""
Checks whether the circular queue is empty or not.
"""
return self.len == 0
def isFull(self) -> bool:
"""
Checks whether the circular queue is full or not.
"""
return self.len == len(self.data)
class Node:
def __init__(self, value, nxt=None):
self.val = value
self.next = nxt
class MyCircularQueue:
""" 64 ms, 92.87% """
def __init__(self, k: int):
self.limit = k
self.head = self.tail = Node(None)
self.len = 0
def enQueue(self, value: int) -> bool:
if self.len < self.limit:
if self.len == 0:
self.head = self.tail = Node(value)
else:
self.head.next = Node(value)
self.head = self.head.next
self.len += 1
return True
return False
def deQueue(self) -> bool:
if self.len:
self.tail = self.tail.next
self.len -= 1
return True
return False
def Front(self) -> int:
if self.len:
return self.tail.val
return -1
def Rear(self) -> int:
if self.len:
return self.head.val
return -1
def isEmpty(self) -> bool:
return self.len == 0
def isFull(self) -> bool:
return self.len == self.limit
# 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()
# @lc code=end