Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3e94ad1

Browse files
Merge pull requestneetcode-gh#3133 from seankit/sean/0695
Create swift/0695-max-area-of-island.swift
2 parents1330547 +327814b commit3e94ad1

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

‎swift/0695-max-area-of-island.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
classSolution{
2+
func maxAreaOfIsland(_ grid:[[Int]])->Int{
3+
letnumRows= grid.count
4+
letnumColumns=grid[0].count
5+
6+
vargrid= grid
7+
varmaxArea=0
8+
9+
forrowin0..<numRows{
10+
forcolumnin0..<numColumns{
11+
guardgrid[row][column]==1else{continue}
12+
13+
letarea=dfs(row: row, column: column, grid:&grid)
14+
maxArea=max(maxArea, area)
15+
}
16+
}
17+
return maxArea
18+
}
19+
20+
privatefunc dfs(row:Int, column:Int, grid:inout[[Int]])->Int{
21+
guard row>=0,
22+
row< grid.count,
23+
column>=0,
24+
column<grid[0].count,
25+
grid[row][column]==1
26+
else{return0}
27+
28+
grid[row][column]=0
29+
30+
return1+ dfs(row: row+1, column: column, grid:&grid)
31+
+ dfs(row: row-1, column: column, grid:&grid)
32+
+ dfs(row: row, column: column+1, grid:&grid)
33+
+ dfs(row: row, column: column-1, grid:&grid)
34+
}
35+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp