设计实现双端队列。
实现 MyCircularDeque 类:
MyCircularDeque(int k) :构造函数,双端队列最大为 k 。boolean insertFront():将一个元素添加到双端队列头部。 如果操作成功返回 true ,否则返回 false 。boolean insertLast() :将一个元素添加到双端队列尾部。如果操作成功返回 true ,否则返回 false 。boolean deleteFront() :从双端队列头部删除一个元素。 如果操作成功返回 true ,否则返回 false 。boolean deleteLast() :从双端队列尾部删除一个元素。如果操作成功返回 true ,否则返回 false 。int getFront() ):从双端队列头部获得一个元素。如果双端队列为空,返回 -1 。int getRear() :获得双端队列的最后一个元素。 如果双端队列为空,返回 -1 。boolean isEmpty() :若双端队列为空,则返回 true ,否则返回 false 。boolean isFull() :若双端队列满了,则返回 true ,否则返回 false 。示例 1:
输入
["MyCircularDeque", "insertLast", "insertLast", "insertFront", "insertFront", "getRear", "isFull", "deleteLast", "insertFront", "getFront"]
[[3], [1], [2], [3], [4], [], [], [], [4], []]
输出
[null, true, true, true, false, 2, true, true, true, 4]
解释
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 <= k <= 10000 <= value <= 1000insertFront, insertLast, deleteFront, deleteLast, getFront, getRear, isEmpty, isFull 调用次数不大于 2000 次Related Topics
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/design-circular-deque
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
数组 or 链表
链表要比数组简单一点
数组:
class MyCircularDeque {
private int[] arr;
private int rear,front;
private int capacity;
public MyCircularDeque(int k) {
capacity = k + 1;
rear = 0;
front = 0;
arr = new int[k + 1];
}
public boolean insertFront(int value) {
if (isFull()){
return false;
}
front = (front - 1 + capacity) % capacity;
arr[front] = value;
return true;
}
public boolean insertLast(int value) {
if (isFull()){
return false;
}
arr[rear] = value;
rear = (rear + 1) % capacity;
return true;
}
public boolean deleteFront() {
if (isEmpty()){
return false;
}
front = (front + 1) % capacity;
return true;
}
public boolean deleteLast() {
if (isEmpty()){
return false;
}
rear = (rear - 1 + capacity) % capacity;
return true;
}
public int getFront() {
if (isEmpty()){
return -1;
}else {
return arr[front];
}
}
public int getRear() {
if (isEmpty()){
return -1;
}else {
return arr[(rear - 1 + capacity) % capacity];
}
}
public boolean isEmpty() {
return rear == front;
}
public boolean isFull() {
return (rear + 1) % capacity == front;
}
}
链表:
class MyCircularDeque {
private class LinkedListNode{
int val;
LinkedListNode prev,next;
LinkedListNode(int val){
this.val = val;
}
}
private LinkedListNode head,tail;//头、尾
private int capacity;//总容量
private int size;//当前长度
public MyCircularDeque(int k) {
capacity = k;
size = 0;
}
public boolean insertFront(int value) {
if (isFull()){
return false;
}
LinkedListNode node = new LinkedListNode(value);
if (size == 0){
head = tail = node;
}else {
node.next = head;
head.prev = node;
head = node;
}
size++;
return true;
}
public boolean insertLast(int value) {
if (isFull()){
return false;
}
LinkedListNode node = new LinkedListNode(value);
if (size == 0){
head = tail = node;
}else {
node.prev = tail;
tail.next = node;
tail = node;
}
size++;
return true;
}
public boolean deleteFront() {
if (isEmpty()){
return false;
}
head = head.next;
size--;
return true;
}
public boolean deleteLast() {
if (isEmpty()){
return false;
}
tail = tail.prev;
size--;
return true;
}
public int getFront() {
if (isEmpty()) return -1;
return head.val;
}
public int getRear() {
if (isEmpty()) return -1;
return tail.val;
}
public boolean isEmpty() {
return size == 0;
}
public boolean isFull() {
return size == capacity;
}
}