当前位置:网站首页>剑指 Offer II 105. 岛屿的最大面积
剑指 Offer II 105. 岛屿的最大面积
2022-07-01 16:28:00 【彼淇梁】
剑指 Offer II 105. 岛屿的最大面积【中等题】
思路:【DFS】
每遇到一个岛,记录岛屿数量为1,同时以这个岛为中心向上下左右四个方向搜索相邻岛屿并记录岛屿数量,同时将这个岛炸沉(置为0),表示已搜索过。
当一个岛四个方向搜索完毕,更新最大岛屿数量,由于每个岛屿面积一样,所以最大岛屿数量即最大岛屿面积。
代码:
class Solution {
static int m;
static int n;
public int maxAreaOfIsland(int[][] grid) {
m = grid.length;
n = grid[0].length;
int ans = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] != 0){
ans = Math.max(ans,dfs(i,j,grid));
}
}
}
return ans;
}
public int dfs(int x,int y,int[][] grid){
if (x < 0 || y < 0 || x == m || y == n || grid[x][y] == 0){
return 0;
}
grid[x][y] = 0;
int[][] f = {
{
0,1},{
0,-1},{
-1,0},{
1,0}};
int cnt = 1;
for (int i = 0; i < 4; i++) {
int dx = x + f[i][0],dy = y + f[i][1];
cnt += dfs(dx,dy,grid);
}
return cnt;
}
}
边栏推荐
- How to cancel automatic search and install device drivers for laptops
- Alibaba cloud, Zhuoyi technology beach grabbing dialogue AI
- UML旅游管理系统「建议收藏」
- 【C语言基础】12 字符串
- 免费抽奖 | 《阿巴豆》探索未来系列盲盒数字版权作品全网首发!
- 数据库系统原理与应用教程(003)—— MySQL 安装与配置:手工配置 MySQL(windows 环境)
- Tutorial on the principle and application of database system (002) -- MySQL installation and configuration: MySQL software uninstallation (Windows Environment)
- 阿里云、追一科技抢滩对话式AI
- 數據庫系統原理與應用教程(006)—— 編譯安裝 MySQL5.7(Linux 環境)
- String类
猜你喜欢
![[observation] where is the consulting going in the digital age? Thoughts and actions of softcom consulting](/img/82/3bb382893682a30e8af130365ec4ef.jpg)
[observation] where is the consulting going in the digital age? Thoughts and actions of softcom consulting
![[JetsonNano] [教程] [入门系列] [三] 搭建TensorFlow环境](/img/0e/52e37527bc717c7a55741725087bad.png)
[JetsonNano] [教程] [入门系列] [三] 搭建TensorFlow环境

Redis 分布式鎖

How to use F1 to F12 correctly on laptop keyboard

Alibaba cloud, Zhuoyi technology beach grabbing dialogue AI

Bugku's file contains

今天14:00 | 港大、北航、耶鲁、清华、加大等15位ICLR一作讲者精彩继续!
![[jetsonnano] [tutorial] [introductory series] [III] build tensorflow environment](/img/0e/52e37527bc717c7a55741725087bad.png)
[jetsonnano] [tutorial] [introductory series] [III] build tensorflow environment

Buuctf gold III

Today, at 14:00, 15 ICLR speakers from Hong Kong University, Beihang, Yale, Tsinghua University, Canada, etc. continue!
随机推荐
Redis Distributed Lock
AI高考志愿填报:大厂神仙打架,考生付费围观
GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
Jojogan practice
Determine whether the linked list is a palindrome linked list
如何使用 etcd 实现分布式 /etc 目录
VMware 虚拟机启动时出现故障:VMware Workstation 与 Hyper-v 不兼容...
Installation and use of sqoop
How to repair the laptop that cannot connect to the wireless network
数据库系统原理与应用教程(001)—— MySQL 安装与配置:MySQL 软件的安装(windows 环境)
【Kotlin】高阶函数介绍
Is it reliable to open an account on flush with mobile phones? Is there any potential safety hazard
VMware 虛擬機啟動時出現故障:VMware Workstation 與 Hyper-v 不兼容...
PR basic clip operation / video export operation
挖财学堂班主任给的证券账户安全吗?能开户吗?
EndeavourOS移动硬盘安装
Go language source level debugger delve
China BMS battery management system Market Research Report (2022 Edition)
Mlperf training v2.0 list released, with the same GPU configuration, the performance of Baidu PaddlePaddle ranks first in the world
[nodemon] app crashed - waiting for file changes before starting... resolvent