[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".
One of the benefits of the circular queue is that we can make use of the spaces in front of the queue. In a normal queue, once the queue becomes full, we cannot insert the next element even if there is a space in front of the queue. But using the circular queue, we can use the space to store new values.
Your implementation should support following operations:
MyCircularQueue(k)
: Constructor, set the size of the queue to be k.Front
: Get the front item from the queue. If the queue is empty, return -1.Rear
: Get the last item from the queue. If the queue is empty, return -1.enQueue(value)
: Insert an element into the circular queue. Return true if the operation is successful.deQueue()
: Delete an element from the circular queue. Return true if the operation is successful.isEmpty()
: Checks whether the circular queue is empty or not.isFull()
: Checks whether the circular queue is full or not.
Example:
MyCircularQueue circularQueue = new MyCircularQueue(3); // set the size to be 3 circularQueue.enQueue(1); // return true circularQueue.enQueue(2); // return true circularQueue.enQueue(3); // return true circularQueue.enQueue(4); // return false, the queue is full circularQueue.Rear(); // return 3 circularQueue.isFull(); // return true circularQueue.deQueue(); // return true circularQueue.enQueue(4); // return true circularQueue.Rear(); // return 4
Note:
- All values will be in the range of [0, 1000].
- The number of operations will be in the range of[1, 1000].
- Please do not use the built-in Queue library.
設計迴圈佇列。
題目就是題意。這是一道設計題,有幾個函式需要實現。
- MyCircularQueue(k) - 一個長度為K的queue
- Front: 讀取佇列頭部的元素,如果佇列為空則返回-1
- Rear: 讀取佇列尾部的元素,如果佇列為空則返回-1
- enQueue(value):從佇列的頭部加入一個元素,如果能加入return true,不能return false
- deQueue():從佇列的尾部加入一個元素,如果能加入return true,不能return false
- isEmpty(): 判斷佇列是否為空
- isFull(): 判斷佇列是否滿了
思路還是比較直觀的,既然是環形佇列,說明一定是隻能使用固定大小的記憶體。為了達到這個題的練習目的,這道題我用陣列實現。建立一個長度為K的陣列,同時建立幾個變數,front, rear是陣列前後的兩個指標,len記錄陣列的長度。
時間O(n)
空間O(n)
Java實現
1 class MyCircularQueue { 2 final int[] a; 3 int front, rear = -1, len = 0; 4 5 public MyCircularQueue(int k) { 6 a = new int[k]; 7 } 8 9 public boolean enQueue(int val) { 10 if (!isFull()) { 11 rear = (rear + 1) % a.length; 12 a[rear] = val; 13 len++; 14 return true; 15 } else 16 return false; 17 } 18 19 public boolean deQueue() { 20 if (!isEmpty()) { 21 front = (front + 1) % a.length; 22 len--; 23 return true; 24 } else 25 return false; 26 } 27 28 public int Front() { 29 return isEmpty() ? -1 : a[front]; 30 } 31 32 public int Rear() { 33 return isEmpty() ? -1 : a[rear]; 34 } 35 36 public boolean isEmpty() { 37 return len == 0; 38 } 39 40 public boolean isFull() { 41 return len == a.length; 42 } 43 } 44 45 /** 46 * Your MyCircularQueue object will be instantiated and called as such: 47 * MyCircularQueue obj = new MyCircularQueue(k); 48 * boolean param_1 = obj.enQueue(value); 49 * boolean param_2 = obj.deQueue(); 50 * int param_3 = obj.Front(); 51 * int param_4 = obj.Rear(); 52 * boolean param_5 = obj.isEmpty(); 53 * boolean param_6 = obj.isFull(); 54 */