Simple C# Solution


  • 0
    W

    Loop through each cell in grid and check it's neighbors

        public int IslandPerimeter(int[,] grid) {
            
            int perimeter = 0;
            
            for(int i=0;i<grid.GetLength(0);i++)
            {
                for(int j=0;j<grid.GetLength(1);j++)
                {
                    if(grid[i,j] == 1)
                    {
                        if(j == 0 || grid[i,j-1] != 1) perimeter++;
                        if(j == grid.GetLength(1) -1 || grid[i, j+1] != 1 ) perimeter++;
                        if(i == 0 || grid[i-1, j] != 1) perimeter++;
                        if(i == grid.GetLength(0) -1 || grid[i+1, j] != 1) perimeter++;
                    }
                }
            }
            
            return perimeter;
        } 
    

Log in to reply
 

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