Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
max-area-of-island.cpp 1.04 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2017-10-11 22:18 +08:00 . Update max-area-of-island.cpp
// Time: O(m * n)
// Space: O(m * n), the max depth of dfs may be m * n
class Solution {
public:
int maxAreaOfIsland(vector<vector<int>>& grid) {
int result = 0;
for (int i = 0; i < grid.size(); ++i) {
for (int j = 0; j < grid[0].size(); ++j) {
int area = 0;
if (dfs(i, j, &grid, &area)) {
result = max(result, area);
}
}
}
return result;
}
private:
bool dfs(const int i, const int j, vector<vector<int>> *grid, int *area) {
static const vector<pair<int, int>> directions{{-1, 0}, { 1, 0},
{ 0, 1}, { 0, -1}};
if (i < 0 || i >= grid->size() ||
j < 0 || j >= (*grid)[0].size() ||
(*grid)[i][j] <= 0) {
return false;
}
(*grid)[i][j] *= -1;
++(*area);
for (const auto& d : directions) {
dfs(i + d.first, j + d.second, grid, area);
}
return true;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助