https://leetcode.com/problems/design-circular-queue/
투 포인터 개념으로 풀면 된다.
class MyCircularQueue:
def __init__(self, k: int):
self.q = [None]*k
self.head = 0
self.tail = 0
self.size = k
def enQueue(self, value: int) -> bool:
if self.q[self.tail] is None:
self.q[self.tail] = value
self.tail = (self.tail + 1) % self.size
return True
return False
def deQueue(self) -> bool:
if self.q[self.head] is not None:
self.q[self.head] = None
self.head = (self.head + 1) % self.size
return True
return False
def Front(self) -> int:
return -1 if self.q[self.head] is None else self.q[self.head]
def Rear(self) -> int:
return -1 if self.q[self.tail - 1] is None else self.q[self.tail - 1]
def isEmpty(self) -> bool:
return self.head == self.tail and self.q[self.head] is None
def isFull(self) -> bool:
return self.head == self.tail and self.q[self.head] is not None
# 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()
'Programming > LeetCode' 카테고리의 다른 글
[LeetCode] Combine Two Tables (MySQL) (0) | 2022.03.31 |
---|---|
[LeetCode] Search a 2D Matrix (0) | 2022.03.30 |
[LeetCode] Implement Queue using Stacks(스택으로 큐 구현) (0) | 2022.03.29 |
[LeetCode] Implement Stack using Queues(큐로 스택 구현) (0) | 2022.03.29 |
[LeetCode] Daily Temperatures (0) | 2022.03.29 |
댓글