题目地址:
https://www.lintcode.com/problem/implement-queue-by-circular-array/description
实现循环队列。题目会初始化一个capacity为 n n n的队列,并保证当队列满的时候是不会执行enqueue操作的。
代码如下:
public class CircularQueue {
int[] arr;
int size;
int front, end;
public CircularQueue(int n) {
// initialize your data structure here
arr = new int[n];
}
/**
* @return: return true if the array is full
*/
public boolean isFull() {
// write your code here
return size == arr.length;
}
/**
* @return: return true if there is no element in the array
*/
public boolean isEmpty() {
// write your code here
return size == 0;
}
/**
* @param element: the element given to be added
* @return: nothing
*/
public void enqueue(int element) {
// write your code here
arr[end] = element;
end = (end + 1) % arr.length;
size++;
}
/**
* @return: pop an element from the queue
*/
public int dequeue() {
// write your code here
int res = arr[front];
front = (front + 1) % arr.length;
size--;
return res;
}
}
所有操作时间复杂度都是 O ( 1 ) O(1) O(1)。