zwkcoding / 100Days-Of-Leetcode

就像电影(500)Days of Summer一样,记录着每一天 Leetcode @itgoyo/500Days-Of-Github
0 stars 0 forks source link

02 Explore00_Queen and Stack_Chapter01 #4

Open zwkcoding opened 5 years ago

zwkcoding commented 5 years ago

Keywords: Queen->FIFO

Colusion :+1:

remenber when you want to process the elements in order, using a queue might be a good choice.

class MyQueue { private: // store elements vector data;
// a pointer to indicate the start position int p_start;
public: MyQueue() {p_start = 0;} / Insert an element into the queue. Return true if the operation is successful. */ bool enQueue(int x) { data.push_back(x); return true; } /* Delete an element from the queue. Return true if the operation is successful. / bool deQueue() { if (isEmpty()) { return false; } p_start++; return true; }; / Get the front item from the queue. */ int Front() { return data[p_start]; }; /* Checks whether the queue is empty or not. / bool isEmpty() { return p_start >= data.size(); } };

int main() { MyQueue q; q.enQueue(5); q.enQueue(3); if (!q.isEmpty()) { cout << q.Front() << endl; } q.deQueue(); if (!q.isEmpty()) { cout << q.Front() << endl; } q.deQueue(); if (!q.isEmpty()) { cout << q.Front() << endl; } }

> queue is ineffiecent! cuz it wastes brackets

- Circular QUEUE C++ Implement
```c++
# Circular QUEUE C++ Implement

class MyCircularQueue {
private:
    // store elements
    vector<int> data;       
    // a pointer to indicate the start position
    int front;
    // a pointer to indicate the start position
    int tail;  
    int size, capacity;
public:
    /** Initialize your data structure here. Set the size of the queue to be k. */
    MyCircularQueue(int k) {
        front = tail = size = 0;
        capacity = k;
        data.clear();
        // no need to push_back later
        for(int i = 0; i < k; i ++)  {
            data.push_back(-1);
        }
    }

    /** Insert an element into the circular queue. Return true if the operation is successful. */
    bool enQueue(int value) {
        if(isFull())  {
            return false;
        } else  {
           data[tail] = value;
            // trick here
           tail = (tail + 1) % data.size();
           size ++;
            return true;
        }
    }

    /** Delete an element from the circular queue. Return true if the operation is successful. */
    bool deQueue() {
        if(isEmpty())  {
            return false;
        } else  {
            front = (front + 1) % data.size();
            size --;
            return true;
        }

    }

    /** Get the front item from the queue. */
    int Front() {
        if(isEmpty())  {
            return -1;
        }
        return data[front];

    }

    /** Get the last item from the queue. */
    int Rear() {
         if(isEmpty())  {
            return -1;
        }
        int index = tail - 1;
        if(index < 0)
            index += data.size();
        return data[index];
    }

    /** Checks whether the circular queue is empty or not. */
    bool isEmpty() {
        return size == 0;
        // return front == tail;

    }

    /** Checks whether the circular queue is full or not. */
    bool isFull() {
        return size == capacity;
        // return (tail + 1) % data.size() == front;

    }
};

/**
 * Your MyCircularQueue object will be instantiated and called as such:
 * MyCircularQueue* obj = new MyCircularQueue(k);
 * bool param_1 = obj->enQueue(value);
 * bool param_2 = obj->deQueue();
 * int param_3 = obj->Front();
 * int param_4 = obj->Rear();
 * bool param_5 = obj->isEmpty();
 * bool param_6 = obj->isFull();
 */

Don't reinvent the wheel! refer to @liuyubobobo/Play-Leetcode-Explore

zwkcoding commented 5 years ago

C++ Tips: