剑指 Offer II 105. 岛屿的最大面积
https://leetcode.cn/problems/ZL6zAn/
var n,m,ans int var dir =[][]int{{1,0},{0,1},{-1,0},{0,-1}} func checkRange(x,y int) bool{ if x>=0&&x<n&&y>=0&&y<m{ return true } return false } func dfs(x,y int,grid [][]int) int{ if checkRange(x,y)&&grid[x][y]==1{ ans++ grid[x][y]=0 } for i:=0;i<4;i++{ dx:=x+dir[i][0] dy:=y+dir[i][1] if checkRange(dx,dy)&&grid[dx][dy]==1{ dfs(dx,dy,grid) } } return ans } func maxAreaOfIsland(grid [][]int) int { n=len(grid);m=len(grid[0]) mxAns:=0 for i:=0;i<n;i++{ for j:=0;j<m;j++{ if grid[i][j]==0{ continue } ans=0 mxAns=getMax(mxAns,dfs(i,j,grid)) } } return mxAns } func getMax(a,b int)int{ if a>b{ return a } return b }
等风起的那一天,我已准备好一切