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)


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