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
| class Solution { int n1 = 0; int n2 = 0; public int islandPerimeter(int[][] grid) { int count = 0; n1 = grid.length; n2 = grid[0].length; for(int i = 0; i < n1; i++){ for(int j = 0; j < n2; j++){ if(grid[i][j] == 1){ count += getCount(grid, i-1, j) + getCount(grid, i+1, j) + getCount(grid, i, j-1) + getCount(grid, i, j+1); } } } return count; }
public int getCount(int[][] grid, int i, int j){ if(i < 0 || i >= n1 || j <0 || j >=n2){ return 1; }else{ if(grid[i][j] == 1){ return 0; }else{ return 1; } } } }
|