Skip to content

Commit 3a3ef88

Browse files
committed
463/kt
1 parent fb6ba0e commit 3a3ef88

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

463.island-perimeter.0.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* @lc app=leetcode id=463 lang=kotlin
3+
*
4+
* [463] Island Perimeter
5+
*/
6+
class Solution_islandPerimeter_0 {
7+
fun islandPerimeter(grid: Array<IntArray>): Int {
8+
val rows = grid.size
9+
if (rows == 0) return 0
10+
val cols = grid[0].size
11+
12+
return (0 until rows).fold(0) { total0, row ->
13+
(0 until cols).fold(total0) { total, col ->
14+
if (grid[row][col] == 0) {
15+
total
16+
} else {
17+
total + 4 -
18+
(if (grid.getOrNull(row - 1)?.getOrNull(col)?:0 == 1) 1 else 0) -
19+
(if (grid.getOrNull(row + 1)?.getOrNull(col)?:0 == 1) 1 else 0) -
20+
(if (grid.getOrNull(row)?.getOrNull(col - 1)?:0 == 1) 1 else 0) -
21+
(if (grid.getOrNull(row)?.getOrNull(col + 1)?:0 == 1) 1 else 0)
22+
23+
}
24+
}
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)