Re: [LeetCode] 刷到面试 Grind169 C++

楼主: SuiseiLeda (星街雷达)   2023-03-18 15:41:33
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);
}
};
作者: oin1104 (是oin的说)   2023-03-18 15:45:00
大师
作者: Rushia (みけねこ的鼻屎)   2023-03-18 15:46:00
问chatGPT
作者: Apache (阿帕契)   2023-03-18 15:48:00
这题不就搜索 DFS
作者: idiont (supertroller)   2023-03-18 15:51:00
flood fill就是一种算法的名字ㄚ 常常用DFS或BFS实作
楼主: SuiseiLeda (星街雷达)   2023-03-18 15:52:00
我很烂 对不起

Links booklink

Contact Us: admin [ a t ] ucptt.com