Clear C# solution


  • 1
    S
        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] == 0) continue;
            		//if at top
            		if (i - 1 == -1 || grid[i - 1, j] == 0) perimeter++;
            		//if at bottom
            		if (i + 1 == grid.GetLength(0) || grid[i + 1, j] == 0) perimeter++;
            		//if at left
            		if (j - 1 == -1 || grid[i, j - 1] == 0) perimeter++;
            		//if at right
            		if (j + 1 == grid.GetLength(1) || grid[i, j + 1] == 0) perimeter++;
            	}
            }
            
            return perimeter;
        }
    }```

Log in to reply
 

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