Leetcode刷题之队列
前言
一个循环队列应当大致具有如下方法
CircularQueue(k): 构造器,设置队列长度为 k 。
Front(): 从队首获取元素。如果队列为空,返回 -1 。
Rear(): 获取队尾元素。如果队列为空,返回 -1 。
enQueue(value): 向循环队列插入一个元素。如果成功插入则返回真。
deQueue(): 从循环队列中删除一个元素。如果成功删除则返回真。
isEmpty(): 检查循环队列是否为空。
isFull(): 检查循环队列是否已满。
开始时头尾指针都指向初始位置,添加元素时,从队尾添加,添加完后队尾指向rear = (rear+1)%capasity
删除元素时,从队头删除,删除完后队首指向front = (front+1)%capasity
1、(622)设计循环队列
设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”
//将当前的队列中的元素数量作为属性,会方便计算
//解法一、数组方式
class MyCircularQueue {
//元素数量,队首指针,队尾指针
int size,front,rear;
int[] arr;
public MyCircularQueue(int k) {
arr = new int[k];
size = 0;
front = 0;
rear = 0;
}
public boolean enQueue(int value) {
if(isFull()){
return false;
}
arr[rear++] = value;
rear %= arr.length;
size++;
return true;
}
public boolean deQueue() {
if(isEmpty()){
return false;
}
front = (front+1)%arr.length;
size--;
return true;
}
public int Front() {
if(isEmpty()){
return -1;
}
return arr[front];
}
public int Rear() {
if(isEmpty()){
return -1;
}
return arr[(arr.length+rear-1)%arr.length];
}
public boolean isEmpty() {
return size==0;
}
public boolean isFull() {
return size==arr.length;
}
}
//解法二、链表方式,删除时head = head.next,插入时tail.next=新节点
class MyCircularQueue {
class Node{
int val;
Node next;
Node(int val){
this.val = val;
next = null;
}
}
Node head,tail;
int count,capacity;
public MyCircularQueue(int k) {
capacity = k;
}
public boolean enQueue(int value) {
if(count==capacity)return false;
Node newNode = new Node(value);
if(count==0){
head = tail = newNode;
}
else{
tail.next = newNode;
tail = newNode;
}
count++;
return true;
}
public boolean deQueue() {
if(count==0)return false;
head = head.next;
count--;
return true;
}
public int Front() {
return count==0?-1:head.val;
}
public int Rear() {
return count==0?-1:tail.val;
}
public boolean isEmpty() {
return count==0;
}
public boolean isFull() {
return count==capacity;
}
}
/**
* Your MyCircularQueue object will be instantiated and called as such:
* MyCircularQueue obj = new MyCircularQueue(k);
* boolean param_1 = obj.enQueue(value);
* boolean param_2 = obj.deQueue();
* int param_3 = obj.Front();
* int param_4 = obj.Rear();
* boolean param_5 = obj.isEmpty();
* boolean param_6 = obj.isFull();
*/
2、(641)设计循环双端队列
设计实现双端队列。
你的实现需要支持以下操作:
MyCircularDeque(k):构造函数,双端队列的大小为k。
insertFront():将一个元素添加到双端队列头部。 如果操作成功返回 true。
insertLast():将一个元素添加到双端队列尾部。如果操作成功返回 true。
deleteFront():从双端队列头部删除一个元素。 如果操作成功返回 true。
deleteLast():从双端队列尾部删除一个元素。如果操作成功返回 true。
getFront():从双端队列头部获得一个元素。如果双端队列为空,返回 -1。
getRear():获得双端队列的最后一个元素。 如果双端队列为空,返回 -1。
isEmpty():检查双端队列是否为空。
isFull():检查双端队列是否满了。
//数组方式
class MyCircularDeque {
int arr[];
int front,rear,size;
/** Initialize your data structure here. Set the size of the deque to be k. */
public MyCircularDeque(int k) {
arr=new int[k];
front=rear=size=0;
}
/** Adds an item at the front of Deque. Return true if the operation is successful. */
public boolean insertFront(int value) {
if(isFull())return false;
front = (arr.length+front-1)%arr.length;
arr[front]=value;
size++;
return true;
}
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
public boolean insertLast(int value) {
if(isFull