-
Notifications
You must be signed in to change notification settings - Fork 1
/
Solution.java
46 lines (34 loc) · 1.03 KB
/
Solution.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
38
39
40
41
42
43
44
45
46
/*
@lc id : 463 Island Perimeter
@author : rohit
@date : 07/07/2020
@url : https://leetcode.com/problems/island-perimeter/
*/
class Solution {
public int oneCellPerimeter(int x, int y, int[][] grid){
int perimeter = 0;
//Up
if(x == 0 || grid[x-1][y] == 0)
perimeter++;
//Down
if(x == grid.length - 1 || grid[x+1][y] == 0)
perimeter++;
//Left
if(y == 0 || grid[x][y-1] == 0)
perimeter++;
//Right
if(y == grid[0].length - 1 || grid[x][y+1] == 0)
perimeter++;
return perimeter;
}
public int islandPerimeter(int[][] grid) {
int perimeter = 0;
for(int row = 0; row < grid.length; row++){
for(int col = 0; col < grid[row].length; col++){
if(grid[row][col] == 1)
perimeter += oneCellPerimeter(row, col, grid);
}
}
return perimeter;
}
}