题目
设计实现双端队列。
你的实现需要支持以下操作:
- MyCircularDeque(k):构造函数,双端队列的大小为k。
- insertFront():将一个元素添加到双端队列头部。如果操作成功返回true。
- insertLast():将一个元素添加到双端队列尾部。如果操作成功返回 true
- deleteFront():从双端队列头部删除一个元素。 如果操作成功返回 true
- deleteLast():从双端队列尾部删除一个元素。如果操作成功返回 true
- getFront():从双端队列头部获得一个元素。如果双端队列为空,返回 -1
- getRear():获得双端队列的最后一个元素。如果双端队列为空,返回 -1
- isEmpty():检查双端队列是否为空。
- isFull():检查双端队列是否满了。
示例:
MyCircularDeque circularDeque = new MycircularDeque(3); // 设置容量大小为3
circularDeque.insertLast(1); // 返回 true
circularDeque.insertLast(2); // 返回 true
circularDeque.insertFront(3); // 返回 true
circularDeque.insertFront(4); // 已经满了,返回 false
circularDeque.getRear(); // 返回 2
circularDeque.isFull(); // 返回 true
circularDeque.deleteLast(); // 返回 true
circularDeque.insertFront(4); // 返回 true
circularDeque.getFront(); // 返回 4
提示:
- 所有值的范围为 [1, 1000]
- 操作次数的范围为 [1, 1000]
- 请不要使用内置的双端队列库。
解法1:数组实现
/**
* 思路:
* 为了头插入和尾插入在一个位置,对他们中的一个要做处理,这里处理头加入。头加入,加入到数组的末尾的位置
* 循环队列队空的条件:head==tail
* 循环队列队满的条件:head==(tail+1)%capacity
* 经过我们处理后,头可以直接获取
* 尾获取需要-1
* 删除头需要+1
* 删除尾需要-1
* 当然上述的这些操作为了防止越界需要做越界处理
*/
class MyCircularDeque {
int arr[];
int head,tail,capacity;
/** Initialize your data structure here. Set the size of the deque to be k. */
public MyCircularDeque(int k) {
capacity=k+1;
arr=new int[capacity];
head=0;
tail=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;
//?每次在head加入一个元素的操作,head需要往他前面的位置加入一个元素(好比tail后移)
head=(head-1+capacity)%capacity;
//这行不能和上面的head赋值互换位置
// 因为这关系到我们deleteFront()操作,
// 还有我们insertLast(int value)操作,两个位置可能会重合,
// 还有我们getFront()操作
arr[head]=value;
return true;
}
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
public boolean insertLast(int value) {
if (isFull())return false;
arr[tail]=value;
tail=(tail+1)%capacity;
return true;
}
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
public boolean deleteFront() {
if (isEmpty())return false;
head=(head+1)%capacity;
return true;
}
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
public boolean deleteLast() {
if (isEmpty())return false;
tail=(tail-1+capacity)%capacity;
return true;
}
/** Get the front item from the deque. */
public int getFront() {
if (isEmpty())return -1;
return arr[head];
}
/** Get the last item from the deque. */
public int getRear() {
if (isEmpty())return -1;
//tail指向的是没有数据的那个位置,所以tail要做处理
return arr[(tail-1+capacity)%capacity];
}
/** Checks whether the circular deque is empty or not. */
public boolean isEmpty() {
return head==tail;
}
/** Checks whether the circular deque is full or not. */
public boolean isFull() {
return (tail+1)%capacity==head;
}
}
解法2:链表实现
/**
* 思路:
* 链表实现
* 设置2个虚拟节点head,tail
* 头插入在head后加节点。尾插入在tail前加节点
* 设置size记录当前链表长度,设置capacity记录我们设置的链表长度
* 队空:size==0
* 队满:size==capacity
*/
class MyCircularDeque {
public class Node{
int value;
Node pre,next;
public Node(int value){
this.value=value;
}
}
Node head,tail;
int size,capacity;
/** Initialize your data structure here. Set the size of the deque to be k. */
public MyCircularDeque(int k) {
head=new Node(-1);
tail=new Node(-1);
head.next=tail;
tail.pre=head;
capacity=k;
size=0;
}
/** Adds an item at the front of Deque. Return true if the operation is successful. */
public boolean insertFront(int value) {
if (size==capacity)return false;
Node node = new Node(value);
node.pre=head;
node.next=head.next;
head.next.pre=node;
head.next=node;
size++;
return true;
}
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
public boolean insertLast(int value) {
if (size==capacity)return false;
Node node = new Node(value);
node.next=tail;
node.pre=tail.pre;
tail.pre.next=node;
tail.pre=node;
size++;
return true;
}
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
public boolean deleteFront() {
if (size==0)return false;
head.next.next.pre=head;
head.next=head.next.next;
size--;
return true;
}
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
public boolean deleteLast() {
if (size==0)return false;
tail.pre.pre.next=tail;
tail.pre=tail.pre.pre;
size--;
return true;
}
/** Get the front item from the deque. */
public int getFront() {
return head.next.value;
}
/** Get the last item from the deque. */
public int getRear() {
return tail.pre.value;
}
/** Checks whether the circular deque is empty or not. */
public boolean isEmpty() {
return size==0;
}
/** Checks whether the circular deque is full or not. */
public boolean isFull() {
return size==capacity;
}
}