leetcode

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

0622.cpp (797B)


      1 class MyCircularQueue {
      2   public:
      3     MyCircularQueue(int k) : n(k), q(k) {}
      4 
      5     bool enQueue(int value) {
      6         if (isFull()) return false;
      7 
      8         q[back++] = value;
      9         if (back >= n) back -= n;
     10         size++;
     11 
     12         return true;
     13     }
     14 
     15     bool deQueue() {
     16         if (isEmpty()) return false;
     17 
     18         if (++front >= n) front -= n;
     19         size--;
     20 
     21         return true;
     22     }
     23 
     24     int Front() const {
     25         if (isEmpty()) return -1;
     26         return q[front];
     27     }
     28 
     29     int Rear() const {
     30         if (isEmpty()) return -1;
     31         if (back == 0) return q.back();
     32         return q[back - 1];
     33     }
     34 
     35     bool isEmpty() const { return size == 0; }
     36 
     37     bool isFull() const { return size == n; }
     38 
     39     const int n;
     40     vector<int> q;
     41 
     42     int front = 0, back = 0;
     43     int size = 0;
     44 };