-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisland-perimeter.java
37 lines (34 loc) · 1.2 KB
/
island-perimeter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution {
public int islandPerimeter(int[][] grid) {
int rows = grid.length;
int cols = grid[0].length;
int perimeter = 0;
// Iterate through each cell in the grid
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
// If the cell is land (grid[i][j] = 1)
if (grid[i][j] == 1) {
// Check the four sides (top, bottom, left, right)
// Increment perimeter if the side is out of bounds or adjacent to water (0)
// Top
if (i == 0 || grid[i - 1][j] == 0) {
perimeter++;
}
// Bottom
if (i == rows - 1 || grid[i + 1][j] == 0) {
perimeter++;
}
// Left
if (j == 0 || grid[i][j - 1] == 0) {
perimeter++;
}
// Right
if (j == cols - 1 || grid[i][j + 1] == 0) {
perimeter++;
}
}
}
}
return perimeter;
}
}