733. Flood Fill easy題
我好爛 我這完全沒想到要怎麼解
你們是寫多少題才能直接自己寫出來啊
class Solution {
public:
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc,
int newcolor) {
if(image[sr][sc] == newcolor) return image;
int m = image.size();
int n = image[0].size();
floodFill(image, sc, sr, m, n, image[sr][sc], newcolor);
return image;
}
private:
void floodFill(vector<vector<int>>& image,
int x, int y, int m, int n, int origincolor, int
newcolor){
if(x<0 || x>=n || y<0 || y>=m) return;
if(image[y][x] != origincolor) return;
image[y][x] = newcolor;
floodFill(image, x+1, y, m, n, origincolor, newcolor);
floodFill(image, x-1, y, m, n, origincolor, newcolor);
floodFill(image, x, y+1, m, n, origincolor, newcolor);
floodFill(image, x, y-1, m, n, origincolor, newcolor);
}
};