-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisland-perimeter.java
More file actions
40 lines (32 loc) · 1.24 KB
/
island-perimeter.java
File metadata and controls
40 lines (32 loc) · 1.24 KB
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
38
39
40
class Solution {
public int islandPerimeter(int[][] grid) {
int count = 0;
for(int row = 0 ; row < grid.length ; row++){
for(int col=0; col < grid[row].length; col++){
int row_length = grid.length;
int col_length = grid[row].length;
if(grid[row][col] == 1 )
{
count = count + 4;
if(row+1 < row_length && grid[row+1][col] == 1)
{
count = count - 1;
}
if(row - 1 >=0 && grid[row-1][col] == 1)
{
count = count - 1;
}
if(col+1 < col_length && grid[row][col+1] == 1)
{
count = count - 1;
}
if(col - 1 >= 0 && grid[row][col-1] == 1)
{
count = count - 1;
}
}
}
}
return count;
}
}