Island Perimeter LeetCode solution in Java

1
class Solution {
2
public int islandPerimeter(int[][] grid) {
3
if (grid == null || grid.length == 0) return 0;
4

5
int row = grid.length, col = grid[0].length;
6
int perimeter = 0;
7

8
for (int i = 0; i < row; i++) {
9
for (int j = 0; j < col; j++) {
10

11
if (grid[i][j] == 1) {
12
perimeter += 4;
13

14
if (i > 0 && grid[i - 1][j] == 1) {
15
perimeter -= 2;
16
}
17

18
if (j > 0 && grid[i][j - 1] == 1) {
19
perimeter -= 2;
20
}
21
}
22
}
23
}
24
return perimeter;
25
}
26
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0