Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
flood-fill.cpp 889 Bytes
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2017-11-26 17:57 +08:00 . Create flood-fill.cpp
// Time: O(m * n)
// Space: O(m * n)
class Solution {
public:
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
int color = image[sr][sc];
if (color == newColor) return image;
dfs(&image, sr, sc, newColor, color);
return image;
}
private:
void dfs(vector<vector<int>> *image, int r, int c, int newColor, int color) {
static const vector<pair<int, int>> directions{{-1, 0}, { 1, 0},
{ 0, 1}, { 0, -1}};
if (r < 0 || r >= image->size() ||
c < 0 || c >= (*image)[0].size() ||
(*image)[r][c] != color) {
return;
}
(*image)[r][c] = newColor;
for (const auto& d : directions) {
dfs(image, r + d.first, c + d.second, newColor, color);
}
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助