LeetCode-622
Design Circular Queue : Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle, and the last position is connected back to the first position to make a circle. It is also called "Ring Buffer".
note : You must solve the problem without using the built-in queue data structure in your programming language.
MyAnswer
:::python
class MyCircularQueue:
def init(self, k: int):
self.deq = collections.deque(maxlen=k)
self.maxlen = k
self.deq_len = 0
def enQueue(self, value: int) -> bool:
if self.deq_len < self.maxlen:
self.deq.append(value)
self.deq_len+=1
return True
else:
return False
def deQueue(self) -> bool:
if self.deq_len > 0:
self.deq.popleft()
self.deq_len-=1
return True
else:
return False
def Front(self) -> int:
if self.deq_len > 0:
return self.deq[0]
else:
return -1
def Rear(self) -> int:
if self.deq_len > 0:
return self.deq[-1]
else:
return -1
def isEmpty(self) -> bool:
if self.deq_len == 0:
return True
else:
return False
def isFull(self) -> bool:
if self.deq_len == self.maxlen:
return True
else:
return False
# 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()
Result : 57ms Memory: 14.4mb
'๐ข One step' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[leetcode-23] Merge k Sorted Lists (0) | 2023.04.09 |
---|---|
[leetcode-641] Design Circular Deque (0) | 2023.04.08 |
[leetcode-232] Implement Queue using Stacks (0) | 2023.04.06 |
[leetcode-225] Implement Queue using Stacks (0) | 2023.04.05 |
[leetcode 739] Daily Temperature (0) | 2023.04.03 |