public static int islandPerimeter(int[][] grid) {
int count = 0;
if ( grid == null || grid[0].length == 0 ) {
return count;
}
for ( int i = 0; i < grid.length; i++ ) {
for ( int j = 0; j < grid[i].length; j++ ) {
if ( grid[i][j] == 1 ) {
if ( j == 0 || (j-1 >=0 && grid[i][j-1] == 0 )) {
count++;
}
if ( i == grid.length-1 || (i+1 <= grid.length-1 && grid[i+1][j] == 0 )) {
count++;
}
if ( j == grid[i].length-1 || (j+1 <= grid[i].length-1 && grid[i][j+1] == 0 )) {
count++;
}
if( i== 0 || ( i-1 >= 0 && grid[i-1][j] == 0 )) {
count++;
}
}
}
}
return count;
}
463. Island Perimeter
最新推荐文章于 2024-07-09 23:43:07 发布