Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions 641. Design Circular Deque
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
class MyCircularDeque {
public:
const int k;
vector<int> q;
int size = 0;
int front = 0;
int rear;
MyCircularDeque(int k): k(k), q(k), rear(k - 1){

}

bool insertFront(int value) {
if(isFull()){
return false;
}
front = (--front + k) % k;
q[front] = value;
size++;
return true;
}

bool insertLast(int value) {
if(isFull()){
return false;
}
rear = ++rear % k;
q[rear] = value;
size++;
return true;
}

bool deleteFront() {
if(isEmpty()){
return false;
}
front = ++front % k;
size--;
return true;
}

bool deleteLast() {
if(isEmpty()){
return false;
}
rear = (--rear + k)%k;
size--;
return true;
}

int getFront() {
return isEmpty() ? -1 : q[front];
}

int getRear() {
return isEmpty() ? -1 : q[rear];
}

bool isEmpty() {
return size == 0;
}

bool isFull() {
return size == k;
}
};
Loading