my java solution, is very clear


  • 0
    Y

    public int islandPerimeter(int[][] grid){
    int perimeter = 0;
    for(int i = 0; i < row; i++){
    for(int j = 0; j < col; j++){
    if(grid[i][j] == 1){
    if(j - 1 < 0 || grid[i][j - 1] == 0) perimeter++;
    if(j + 1 >= col || grid[i][j + 1] == 0) perimeter++;
    if(i - 1 < 0 || grid[i - 1][j] == 0) perimeter++;
    if(i + 1 >= row || grid[i + 1][j] == 0) perimeter++;
    }
    }
    }
    return perimeter;
    }

    i calculate the perimeter by the neiborhoods, if a ceil == 1, and its (left or right or top or buttom) neiborhood is water
    ,perimeter++.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.