easy java solution


  • -1
    R

    if “1” add a neighborhood ,Perimeter-2.
    public class Solution {
    public int islandPerimeter(int[][] grid) {
    int res=0;
    for(int i=0;i<grid.length;i++){
    for(int j=0;j<grid[i].length;j++){
    if(grid[i][j]==1){
    res+=4;
    if(j!=0&&grid[i][j-1]==1)
    res-=2;
    if(i!=0&&grid[i-1][j]==1)
    res-=2;
    }

        	}
        }
        return res;
    }
    

    }


  • 0
    W

    @rick132134

    Input ``` before your code


Log in to reply
 

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