Simple C# solution


  • 0
    D

    Simply, count the sea to land transitions .. while looping the array ..

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

Log in to reply
 

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