마지막 원소를 구하면 다시 첫 원소를 탐색하는 링 버퍼를 구현하는 문제
https://leetcode.com/problems/design-circular-queue/
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.__queueSize = k
self.__head = 0
self.__tail = -1
self.__count = 0
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
else:
if self.__queueSize == self.__tail + 1:
self.__tail = 0
else:
self.__tail += 1
self.__queue[ self.__tail ] = value
self.__count += 1
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
else:
if self.__queueSize == self.__head + 1:
self.__head = 0
else:
self.__head += 1
self.__count -= 1
return True
def Front(self) -> int:
"""
Get the front item from the queue.
"""
if self.isEmpty():
return -1
else:
return self.__queue[ self.__head ]
def Rear(self) -> int:
"""
Get the last item from the queue.
"""
if self.isEmpty():
return -1
else:
return self.__queue[ self.__tail ]
def isEmpty(self) -> bool:
"""
Checks whether the circular queue is empty or not.
"""
return not self.__count
def isFull(self) -> bool:
"""
Checks whether the circular queue is full or not.
"""
return self.__count == self.__queueSize
# 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()
'코드' 카테고리의 다른 글
Open the Lock (0) | 2019.11.04 |
---|---|
Number of Islands (0) | 2019.11.04 |
Unique Binary Search Trees II (0) | 2019.10.25 |
K-th Symbol in Grammar (0) | 2019.10.24 |
Merge Two Sorted Lists (0) | 2019.10.24 |