c# solution


  • 0
    M
    public int IslandPerimeter(int[,] grid) {
               if (grid == null) return 0;
    
                int result = 0;
                int outerSize = grid.GetLength(0);
                int innerSize = grid.GetLength(1);
    
                for (int i = 0; i < outerSize; i++)
                {
                    for (int j = 0; j < innerSize; j++)
                    {
                        if (grid[i, j] == 0) continue;
                        if (j < 1 || grid[i, j - 1] == 0) result++;
                        if (j + 1 >= innerSize || grid[i, j + 1] == 0) result++;
                        if (i < 1 || grid[i - 1, j] == 0) result++;
                        if (i + 1 >= outerSize || grid[i + 1, j] == 0) result++;
                    }
                }
    
                return result;
        }
    

Log in to reply
 

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