題幹: https://leetcode.com/problems/design-circular-deque/
這題跟622類似,算是它的進階版,要支持雙向的queue,大部分代碼我都複用622的方案,
唯獨要注意的一點是deleteLast和deleteFront要注意邊界條件,就是當刪完之後隊列爲空時,不要移動currentHead或者currentTail指針位置。
代碼如下:
package com.example.demo.leetcode;
import java.util.Arrays;
class MyCircularDeque {
int[] content;
int currentTail = 0;
int currentHead = 0;
/** Initialize your data structure here. Set the size of the deque to be k. */
public MyCircularDeque(int k) {
content = new int[k];
Arrays.fill(content, -1);
}
private int previousPointer(int index){
int p = index-1;
if(p<0){
p = p+content.length;
}
return p;
}
/** Adds an item at the front of Deque. Return true if the operation is successful. */
public boolean insertFront(int value) {
if(isFull()){
return false;
}
if(content[currentHead]==-1){
content[currentHead] = value;
return true;
}else{
currentHead=previousPointer(currentHead);
content[currentHead] = value;
return true;
}
}
private int nextPointer(int currentPointer){
int p = currentPointer+1;
if(p>content.length-1){
p=p%content.length;
}
return p;
}
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
public boolean insertLast(int value) {
if(isFull()){
return false;
}
if(content[currentTail]==-1){
content[currentTail] = value;
return true;
}else{
currentTail = nextPointer(currentTail);
content[currentTail]= value;
return true;
}
}
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
public boolean deleteFront() {
if(isEmpty()){
return false;
}
content[currentHead]=-1;
if(content[nextPointer(currentHead)]!=-1){
currentHead=nextPointer(currentHead);
}
return true;
}
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
public boolean deleteLast() {
if(isEmpty()){
return false;
}
content[currentTail] = -1;
if(content[previousPointer(currentTail)]!=-1){
currentTail = previousPointer(currentTail);
}
return true;
}
/** Get the front item from the deque. */
public int getFront() {
return content[currentHead];
}
/** Get the last item from the deque. */
public int getRear() {
return content[currentTail];
}
/** Checks whether the circular deque is empty or not. */
public boolean isEmpty() {
for(int i=0;i<content.length;i++){
if(content[i]!=-1){
return false;
}
}
return true;
}
/** Checks whether the circular deque is full or not. */
public boolean isFull() {
for(int i=0;i<content.length;i++){
if(content[i]==-1){
return false;
}
}
return true;
}
public static void main(String[] args) {
MyCircularDeque obj = new MyCircularDeque(77);
boolean param_1 = obj.insertFront(89);
int param_2 = obj.getRear();
boolean param_3 = obj.deleteLast();
int param_4 = obj.getRear();
boolean param_5 = obj.insertFront(19);
boolean param_6 = obj.insertFront(23);
boolean param_7 = obj.insertFront(23);
boolean param_8 = obj.insertFront(82);
boolean param_9 = obj.isFull();
boolean param_10 = obj.insertFront(45);
boolean param_11 = obj.isFull();
int param_12 = obj.getRear();
// expect 19
System.out.println(param_12);
}
}