算法
(DFS) $O(n*m)$
DFS,从起点开始枚举4个方向并填充颜色
C++ 代码
class Solution {
public:
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
if (image[sr][sc] == newColor) {
return image;
}
fill(image, sr, sc, image[sr][sc], newColor);
return image;
}
void fill(vector<vector<int>> &image, int sr, int sc, int color, int newColor) {
if (sr < 0 || sr >= image.size() || sc < 0 || sc >= image[0].size() || image[sr][sc] != color) {
return;
}
image[sr][sc] = newColor;
fill(image, sr + 1, sc, color, newColor);
fill(image, sr - 1, sc, color, newColor);
fill(image, sr, sc + 1, color, newColor);
fill(image, sr, sc - 1, color, newColor);
}
};