No tricks java solution


  • 0
    A
    public int islandPerimeter(int[][] grid) {
            int peri = 0;
            for(int i=0; i<grid.length; i++){
                for(int j=0; j<grid[i].length; j++){
                    if(grid[i][j]==1){
                        //For edges
                        if(i==0) peri++;
                        if(i==grid.length-1) peri++;
                        if(j==0) peri++;
                        if(j==grid[i].length-1) peri++;
                        //Check four sides for non edge cells
                        if(i+1<grid.length&&grid[i+1][j]==0) peri++;
                        if(i-1>=0&&grid[i-1][j]==0) peri++;
                        if(j+1<grid[i].length&&grid[i][j+1]==0) peri++;
                        if(j-1>=0&&grid[i][j-1]==0) peri++;
                    }
                }
            }
            return peri;
        }
    

Log in to reply
 

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