Skip to content

Commit fab97a6

Browse files
committed
(#463)IslandPerimeter
1 parent 32539c2 commit fab97a6

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int islandPerimeter(int[][] grid) {
3+
int island = 0;
4+
int neighbour = 0;
5+
for(int i = 0; i < grid.length; i ++){
6+
for(int j = 0; j < grid[i].length; j ++){
7+
if(grid[i][j] == 1)
8+
{
9+
island ++;
10+
if(j + 1 < grid[i].length && grid[i][j + 1] == 1) neighbour ++;
11+
if(i + 1 < grid.length && grid[i + 1][j] == 1) neighbour++;
12+
}
13+
}
14+
}
15+
return island * 4 - neighbour * 2;
16+
}
17+
}

0 commit comments

Comments
 (0)