LeetCode 622. Design Circular Queue(设计循环队列)

题目要求

设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 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

提示:

所有的值都在 1 至 1000 的范围内;
操作数将在 1 至 1000 的范围内;
请不要使用内置的队列库。

设计思路

什么是循环队列:在用数组实现的队列中,循环队列和普通队列的区别是,普通队列的队头出队之后它前面的空间就不能使用了,而循环队列可以重复利用这些空间,比如当队尾的位置达到队列长度时,它的下一个元素可以从数组的第一个位置继续从前往后开始摆放。
下图队空:
LeetCode 622. Design Circular Queue(设计循环队列)_第1张图片
LeetCode 622. Design Circular Queue(设计循环队列)_第2张图片
LeetCode 622. Design Circular Queue(设计循环队列)_第3张图片
下图队满:
LeetCode 622. Design Circular Queue(设计循环队列)_第4张图片
front:指向队头位置,每当有一个元素出队,指针后移一位。
rear:指向队尾的后一个位置,每当有一个元素入队,它就会后移一位。
循环:front和rear都会在队列中循环移动,数组最后一个位置的下一个位置就是数组的第一个位置。
判断队空:rear等于front。我们知道开始的时候两个指针是在一起的,即rear=front,当有元素入队时,rear就会加1,当有元素出队时,front就会加1,当front追上rear时,队列中没有元素,此时队空。队空的公式:front=rear;
判断队满:当rear不断加1,到达了数组的尾部,就会从数组的第一个位置继续开始移动,当它移动到front的前一个的位置时,队满,虽然此时rear指向的位置没有数据,但我们还是判断这是队满,这是为了和队空进行区分。队满的公式:front=(rear+1)%size,此size表示数组的长度。
更新front和rear的方法
front = (front + 1) % size;
rear = (rear + 1) % size;
算法实现

class MyCircularQueue {
	int[] array;
	int front=0;
	int rear=0;
	int size;	
    /** Initialize your data structure here. Set the size of the queue to be k. */
    public MyCircularQueue(int k) {
        this.array=new int[k+1];
        this.size=k+1;
    }    
    /** Insert an element into the circular queue. Return true if the operation is successful. */
    public boolean enQueue(int value) {
        if(front==(rear+1)%size){
        	return false;
        }else{
        	array[rear]=value;
        	rear=(rear+1)%size;
        	return true;
        }
    }    
    /** Delete an element from the circular queue. Return true if the operation is successful. */
    public boolean deQueue() {
        if(front==rear){
        	return false;
        }else{
        	front=(front+1)%size;  
        	return true;
        }
    }    
    /** Get the front item from the queue. */
    public int Front() {
        if(front==rear){
        	return -1;
        }else{
        	return array[front];
        }
    }    
    /** Get the last item from the queue. */
    public int Rear() {
        if(front==rear){
        	return -1;
        }else{
        	if(rear==0){
        		return array[size-1];
        	}else{
        		return array[rear-1];
        	}
        }
    }    
    /** Checks whether the circular queue is empty or not. */
    public boolean isEmpty() {
        if(front==rear){
        	return true;
        }else{
        	return false;
        }
    }    
    /** Checks whether the circular queue is full or not. */
    public boolean isFull() {
        if(front==(rear+1)%size){
        	return true;
        }else{
        	return false;
        }
    }
}

你可能感兴趣的:(LeetCode)