-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathIslandPerimeter.java
67 lines (61 loc) · 1.85 KB
/
IslandPerimeter.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
60
61
62
63
64
65
66
67
/*https://leetcode.com/problems/island-perimeter/*/
class Solution {
public int islandPerimeter(int[][] grid)
{
int count = 0;
for (int i = 0; i < grid.length; i++)
{
for (int j = 0; j < grid[0].length; j++)
{
if (grid[i][j] == 1)
{
int res = 0;
res += check(i-1,j,grid.length, grid[0].length, grid);
res += check(i+1,j,grid.length, grid[0].length, grid);
res += check(i,j+1,grid.length, grid[0].length, grid);
res += check(i,j-1,grid.length, grid[0].length, grid);
count += res;
}
}
}
return count;
}
public int check(int i, int j, int n, int m, int [][] grid)
{
if (i < 0 || j < 0 || i >= n || j >= m)
return 1;
else if (grid[i][j] == 0)
return 1;
return 0;
}
}
class Solution {
public int islandPerimeter(int[][] grid) {
int n = grid.length;
int m = grid[0].length;
int count = 0;
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
if(grid[i][j] == 1) {
//Top
if(i==0 || grid[i-1][j] == 0) {
count++;
}
//Left
if(j == 0 || grid[i][j-1] == 0) {
count++;
}
//Down
if(i == n-1 || grid[i+1][j] == 0) {
count++;
}
//Right
if(j == m-1 || grid[i][j+1] == 0) {
count++;
}
}
}
}
return count;
}
}