Leetcode 622.設計循環隊列(Design Circular Queue)

Leetcode 622.設計循環隊列

1 題目描述(Leetcode題目鏈接

  設計你的循環隊列實現。 循環隊列是一種線性數據結構,其操作表現基於 FIFO(先進先出)原則並且隊尾被連接在隊首之後以形成一個循環。它也被稱爲“環形緩衝器”。

循環隊列的一個好處是我們可以利用這個隊列之前用過的空間。在一個普通隊列裏,一旦一個隊列滿了,我們就不能插入下一個元素,即使在隊列前面仍有空間。但是使用循環隊列,我們能使用這些空間去存儲新的值。

你的實現應該支持如下操作:

  • MyCircularQueue(k): 構造器,設置隊列長度爲 k 。
  • Front: 從隊首獲取元素。如果隊列爲空,返回 -1 。
  • Rear: 獲取隊尾元素。如果隊列爲空,返回 -1 。
  • enQueue(value): 向循環隊列插入一個元素。如果成功插入則返回真。
  • deQueue(): 從循環隊列中刪除一個元素。如果成功刪除則返回真。
  • isEmpty(): 檢查循環隊列是否爲空。
  • isFull(): 檢查循環隊列是否已滿。
MyCircularQueue circularQueue = new MyCircularQueue(3); // 設置長度爲 3
circularQueue.enQueue(1);  // 返回 true
circularQueue.enQueue(2);  // 返回 true
circularQueue.enQueue(3);  // 返回 true
circularQueue.enQueue(4);  // 返回 false,隊列已滿
circularQueue.Rear();  // 返回 3
circularQueue.isFull();  // 返回 true
circularQueue.deQueue();  // 返回 true
circularQueue.enQueue(4);  // 返回 true
circularQueue.Rear();  // 返回 4

提示:

  • 所有的值都在 0 至 1000 的範圍內;
  • 操作數將在 1 至 1000 的範圍內;
  • 請不要使用內置的隊列庫。

2 題解

  用數組或鏈表實現都可以,這裏使用數組實現,我們令數組最大長度爲k+1k+1,設置頭尾指針,頭指針指向隊頭,尾指針指向隊尾的下一個位置。這樣就可以實現,如果隊列爲空,則頭尾指針指向同一個位置,如果隊列滿則尾指針的下一個位置就是頭指針。另外在移動頭尾指針時,由於是循環的,因此移動時用下面這個式子就可以了:
head(tail)=(head(tail)+1)%(k+1) head(tail) = (head(tail)+ 1)\%(k+1)

class MyCircularQueue:

    def __init__(self, k: int):
        """
        Initialize your data structure here. Set the size of the queue to be k.
        """
        self.head = 0
        self.tail = 0
        self.k = k + 1
        self.queue = [-1]*self.k
        

    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
        self.queue[self.tail] = value
        self.tail = (self.tail + 1) % self.k
        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
        self.queue[self.head] = -1
        self.head = (self.head + 1) % self.k
        return True
        

    def Front(self) -> int:
        """
        Get the front item from the queue.
        """
        return self.queue[self.head]
        

    def Rear(self) -> int:
        """
        Get the last item from the queue.
        """
        return self.queue[self.tail - 1]
        

    def isEmpty(self) -> bool:
        """
        Checks whether the circular queue is empty or not.
        """
        return self.head == self.tail
        

    def isFull(self) -> bool:
        """
        Checks whether the circular queue is full or not.
        """
        return (self.tail + 1) % self.k == self.head
        


# 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()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章