算法 day8

隊列和棧

循環隊列

class MyCircularQueue {
public:
    int q[1005];
    int left;
    int right;
    int size;
    int ccount;
    /** Initialize your data structure here. Set the size of the queue to be k. */
    MyCircularQueue(int k) {
        left = 0;
        right = 0;
        size = k;
        ccount = 0;
    }
    
    /** Insert an element into the circular queue. Return true if the operation is successful. */
    bool enQueue(int value) {
        if(isFull())return false;
        q[right++] = value;
        right %= size;
        ccount++;
        return true;
    }
    
    /** Delete an element from the circular queue. Return true if the operation is successful. */
    bool deQueue() {
        if(isEmpty())return false;
        left++;
        left %= size;
        ccount--;
        return true;
    }
    
    /** Get the front item from the queue. */
    int Front() {
        if(!isEmpty())return q[left];
        return -1;
    }
    
    /** Get the last item from the queue. */
    int Rear() {
        if(!isEmpty())return q[(right-1+size)%size];
        return -1;
    }
    
    /** Checks whether the circular queue is empty or not. */
    bool isEmpty() {
        if(ccount==0)return true;
        else return false;
    }
    
    /** Checks whether the circular queue is full or not. */
    bool isFull() {
        if(ccount==size)
        return true;
        else return false;
    }
};

/**
 * 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();
 */

隊列實現滑動窗口

class MovingAverage {
public:
    /** Initialize your data structure here. */
    int k;
    int maxk ;
    queue<int> q;
    double sum;
    MovingAverage(int size) {
        k = 0;
        maxk = size;
        sum =  0;
    }
    
    double next(int val) {
        if(k<maxk)
        {
            k++;
            sum+=val;
            q.push(val);
            return (sum/k);
        }
        sum -= q.front();
        q.pop();
        sum+=val;
        q.push(val);
        return sum/k;
    }
};

/**
 * Your MovingAverage object will be instantiated and called as such:
 * MovingAverage* obj = new MovingAverage(size);
 * double param_1 = obj->next(val);
 */

用隊列實現bfs

 牆與門 超時

class Solution {
public:
    bool isvalid(int x,int y, int n,int m,vector<vector<int>> rooms)
    {
        return (x>=0&&x<n&&y>=0&&y<m&&rooms[x][y]>0);
    }
    void wallsAndGates(vector<vector<int>>& rooms) {
        int n = rooms.size();
        if(n==0)return ;
        int m = rooms[0].size();
        queue<pair<int,int>> q;
        int visit[n][m];
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(rooms[i][j] == 0)
                {
                    memset(visit,0,sizeof(visit));
                    visit[i][j] = 1;
                    q.push({i,j});
                    while(!q.empty())
                    {
                        pair<int,int> p = q.front();
                        q.pop();
                        int d[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
                        for(int dd = 0;dd<4;dd++)
                        {
                            int newx = p.first+d[dd][0];
                            int newy = p.second+d[dd][1];
                            if(isvalid(newx,newy,n,m,rooms)&&!visit[newx][newy])
                            {
                                visit[newx][newy] = 1;
                                rooms[newx][newy] = min(rooms[p.first][p.second]+1,rooms[newx][newy]);
                                q.push({newx,newy});
                            }
                        }
                    }
                }
            }
        }
    }
};

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章