You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
class MyCircularDeque {
public:
/** Initialize your data structure here. Set the size of the deque to be k. */
MyCircularDeque(int k) {
size = k;
}
/** Adds an item at the front of Deque. Return true if the operation is successful. */
bool insertFront(int value) {
if (isFull()) return false;
data.insert(data.begin(), value);
return true;
}
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
bool insertLast(int value) {
if (isFull()) return false;
data.push_back(value);
return true;
}
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
bool deleteFront() {
if (isEmpty()) return false;
data.erase(data.begin());
return true;
}
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
bool deleteLast() {
if (isEmpty()) return false;
data.pop_back();
return true;
}
/** Get the front item from the deque. */
int getFront() {
if (isEmpty()) return -1;
return data.front();
}
/** Get the last item from the deque. */
int getRear() {
if (isEmpty()) return -1;
return data.back();
}
/** Checks whether the circular deque is empty or not. */
bool isEmpty() {
return data.empty();
}
/** Checks whether the circular deque is full or not. */
bool isFull() {
return data.size() >= size;
}
private:
vector<int> data;
int size;
};
class MyCircularDeque {
public:
/** Initialize your data structure here. Set the size of the deque to be k. */
MyCircularDeque(int k) {
size = k; head = k - 1; tail = 0, cnt = 0;
data.resize(k);
}
/** Adds an item at the front of Deque. Return true if the operation is successful. */
bool insertFront(int value) {
if (isFull()) return false;
data[head] = value;
head = (head - 1 + size) % size;
++cnt;
return true;
}
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
bool insertLast(int value) {
if (isFull()) return false;
data[tail] = value;
tail = (tail + 1) % size;
++cnt;
return true;
}
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
bool deleteFront() {
if (isEmpty()) return false;
head = (head + 1) % size;
--cnt;
return true;
}
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
bool deleteLast() {
if (isEmpty()) return false;
tail = (tail - 1 + size) % size;
--cnt;
return true;
}
/** Get the front item from the deque. */
int getFront() {
return isEmpty() ? -1 : data[(head + 1) % size];
}
/** Get the last item from the deque. */
int getRear() {
return isEmpty() ? -1 : data[(tail - 1 + size) % size];
}
/** Checks whether the circular deque is empty or not. */
bool isEmpty() {
return cnt == 0;
}
/** Checks whether the circular deque is full or not. */
bool isFull() {
return cnt == size;
}
private:
vector<int> data;
int size, head, tail, cnt;
};
Activity