[Leetcode] 622. Design Circular Queue
2021. 1. 18. 14:46ㆍProblem Solving/LeetCode
Leetcode 622. Design Circular Queue
(3) Design Circular Queue - LeetCode
Solution
class MyCircularQueue:
def __init__(self, k: int):
self.q = [None] * (k+1)
self.max = k + 1
self.front = 0
self.rear = 0
def enQueue(self, value: int) -> bool:
if self.isFull():
return False
else:
self.rear = (self.rear + 1) % self.max
self.q[self.rear] = value
return True
def deQueue(self) -> bool:
if self.isEmpty():
return False
else:
self.q[self.front] = None
self.front = (self.front + 1) % self.max
return True
def Front(self) -> int:
if self.isEmpty():
return -1
else:
return self.q[(self.front + 1) % self.max]
def Rear(self) -> int:
if self.isEmpty():
return -1
else:
return self.q[self.rear]
def isEmpty(self) -> bool:
return self.front == self.rear
def isFull(self) -> bool:
return self.front == (self.rear + 1) % self.max
# 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()
배열을 이용한 풀이다.
Reference
파이썬 알고리즘 인터뷰
|
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
[Leetcode] 706. Design HashMap (0) | 2021.01.19 |
---|---|
[Leetcode] 641. Design Circular Deque (0) | 2021.01.19 |
[Leetcode] 232. Implement Queue using Stacks (0) | 2021.01.18 |
[Leetcode] 225. Implement Stack using Queues (0) | 2021.01.18 |
[Leetcode] 739. Daily Temperatures (0) | 2021.01.18 |