1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class Solution { public: vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) { dfs(image, newColor, image[sr][sc], sr, sc); return image; } private: void dfs(vector<vector<int>>& image, int newColor, int oldColor, int i, int j) { if(i < 0 || i > image.size() - 1 || j < 0 || j > image[0].size() - 1 || image[i][j] != oldColor || image[i][j] == newColor) return; image[i][j] = newColor; dfs(image, newColor, oldColor, i + 1, j); dfs(image, newColor, oldColor, i - 1, j); dfs(image, newColor, oldColor, i, j - 1); dfs(image, newColor, oldColor, i, j + 1); } };
|