Leetcode 733. 圖像渲染 floodFill算法

 

const int dx[] = {1,0,-1,0};
const int dy[] = {0,1,0,-1};
class Solution {
public:
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
        int oldColor = image[sr][sc];
        if(oldColor==newColor) return image;
        image[sr][sc] = newColor;
        for(int i=0;i<4;i++){
            int x = sr + dx[i];
            int y = sc + dy[i];
            if(x>=0&&x<image.size()&&y>=0&&y<image[0].size()&&image[x][y]==oldColor){
                floodFill(image,x,y,newColor);
            }
        }
        return image;
    }
};

 

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