-
Notifications
You must be signed in to change notification settings - Fork 0
/
Island_Perimeter_463.java
59 lines (50 loc) · 1.21 KB
/
Island_Perimeter_463.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
47
48
49
50
51
52
53
54
55
56
57
58
59
package leetcode;
class Solution463 {
public int islandPerimeter(int[][] grid) {
int perimeter = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == 1) {
perimeter += getCurrentPeri(i, j, grid);
}
}
}
return perimeter;
}
public int getCurrentPeri(int row, int col, int[][] grid) {
int up = row - 1, down = row + 1, left = col - 1, right = col + 1;
int count = 0;
// checking the up direction
if (up >= 0) {
if (grid[up][col] == 1) {
count++;
}
}
// checking the down direction
if (down < grid.length) {
if (grid[down][col] == 1) {
count++;
}
}
// checking the left direction
if (left >= 0) {
if (grid[row][left] == 1) {
count++;
}
}
// checking the right direction
if (right < grid[0].length) {
if (grid[row][right] == 1) {
count++;
}
}
return (4 - count);
}
}
public class Island_Perimeter_463 {
public static void main(String[] args) {
int[][] grid = { { 0, 1, 0, 0 }, { 1, 1, 1, 0 }, { 0, 1, 0, 0 }, { 1, 1, 0, 0 } };
Solution463 ns = new Solution463();
System.out.println(ns.islandPerimeter(grid));
}
}