sample java solution


  • 0

    '''
    public int islandPerimeter(int[][] grid) {
    int pCount = 0;
    for(int i=0; i<grid.length; i++){
    for(int j=0; j<grid[i].length; j++){

                if (grid[i][j] == 1){
                    
                    if(j-1 >= 0)
                        pCount += grid[i][j-1] == 0 ? 1:0;
                    else
                        pCount++;
                    if(j+1 < grid[i].length)
                        pCount += grid[i][j+1] == 0 ? 1:0;
                    else
                        pCount++;
                    if(i-1 >= 0)
                        pCount += grid[i-1][j] == 0 ? 1:0;
                    else
                        pCount++;
                    if(i+1 < grid.length)
                        pCount += grid[i+1][j] == 0 ? 1:0;
                    else
                        pCount++;
                }
                
            }
        }
        
        return pCount;
    }
    

    ''''


Log in to reply
 

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