File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments