[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  */

 

LeetCode 题目总结

你可能感兴趣的:([LeetCode] 622. Design Circular Queue)