827.最大人工岛

发布时间:2026/11/6 0:17:49
827.最大人工岛 链接827. 最大人工岛 - 力扣LeetCode题解1、grid[i][j] 1 的节点visited里面存储的访问相同的岛屿标记为相同的tag2、counts记录的tag2count这个岛屿的数量是多大3、grid[i][j] 0的节点访问周围4个访问并且记录当前4周访问过的tags将当前岛屿的数量进行累加class Solution { public: int largestIsland(vectorvectorint grid) { int m grid.size(); if (m 0) { return 0; } int n grid[0].size(); if (n 0) { return 0; } vectorvectorint visited(m, vectorint(n, -1)); unordered_mapint, int counts; int result 0; for (int i 0; i m; i) { for (int j 0; j n; j) { if (grid[i][j] 1 visited[i][j] -1) { int tag i * n j; counts[tag] bfs(i, j, m, n, grid, visited); result max(result, counts[tag]); } } } if (result 0) { return 1; } for (int i 0; i m; i) { for (int j 0; j n; j) { if (grid[i][j] 0) { int tmp 1; unordered_setint tags; for (auto d : direc) { int next_x i d[0]; int next_y j d[1]; if (next_x 0 || next_y 0 || next_x m || next_y n || grid[next_x][next_y] ! 1 || tags.find(visited[next_x][next_y]) ! tags.end()) { continue; } tmp counts[visited[next_x][next_y]]; tags.insert(visited[next_x][next_y]); } result max(result, tmp); } } } return result; } vectorvectorint direc{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; int bfs(int x, int y, int m, int n, const vectorvectorint grid, vectorvectorint visited) { queueint que; que.push(x * n y); int tag x * n y; visited[x][y] tag; int result 1; while (!que.empty()) { auto f que.front(); que.pop(); for (auto d : direc) { int next_x f / n d[0]; int next_y f % n d[1]; if (next_x 0 || next_y 0 || next_x m || next_y n || visited[next_x][next_y] ! -1 || grid[next_x][next_y] ! 1) { continue; } visited[next_x][next_y] tag; que.push(next_x * n next_y); result; } } return result; } };

关于本文作者

来自尧图内容编辑团队

尧图内容编辑团队 内容团队

尧图内容编辑团队

本文由尧图网络内容编辑团队执笔。团队由资深项目经理、前端工程师与设计师组成,所有内容均来自亲手交付的真实项目,先讲清问题、再给出可落地的解法。尧图深耕北京网站建设十年,服务过京华建材集团、智造科技等各行业客户,把一线经验沉淀为可复用的行业观察。

  • 十年建站经验,覆盖建材、制造、服务、文创等
  • 项目经理把关选题与事实准确性
  • 工程师与设计师联合撰写专业细节
  • 统一编辑规范,保证文风与排版一致
  • 每月复盘转化数据,迭代选题方向

延伸阅读

相关资讯与近期热门内容

深度阅读推荐

建站决策前值得细读的三篇

网站改版的5个关键决策
2024-08-12

网站改版的5个关键决策

什么时候该改版、改到什么程度、如何避免流量掉光,京华建材集团改版复盘给出答案。

获取专属建站方案

看完文章,把您的行业与预算告诉我们,免费获取一份量身定制的官网建设方案与报价。

立即免费咨询