leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0641.cpp (999B)


0 class MyCircularDeque {
1 const int n;
2 vector<int> q;
4 int size = 0;
5 int head = 0, tail = 0;
7 public:
8 MyCircularDeque(int k) : n(k), q(k) {}
10 bool insertFront(int value) {
11 if (size == n) return false;
12 q[head] = value;
13 head = (head + 1) % n;
14 size++;
15 return true;
16 }
18 bool insertLast(int value) {
19 if (size == n) return false;
20 tail = (tail - 1 + n) % n;
21 q[tail] = value;
22 size++;
23 return true;
24 }
26 bool deleteFront() {
27 if (!size) return false;
28 head = (head - 1 + n) % n;
29 size--;
30 return true;
31 }
33 bool deleteLast() {
34 if (!size) return false;
35 tail = (tail + 1) % n;
36 size--;
37 return true;
38 }
40 int getFront() const { return size ? q[(head - 1 + n) % n] : -1; }
41 int getRear() const { return size ? q[tail] : -1; }
42 bool isEmpty() const { return size == 0; }
43 bool isFull() const { return size == n; }
44 };