C# Solution


  • 0
    S
    public int IslandPerimeter(int[,] grid) {
            int row = grid.GetLength(0);
    			int column = grid.GetLength(1);
    			int result = 0;
    			for (int i = 0; i < row; i++)
    			{
    				for (int j = 0; j < column; j++)
    				{
    					if (grid[i, j] == 1)
    					{
    						result += FindParameter(grid, i, j);
    					}
    				}
    			}
    
    			return result;    
        }
        
        private int FindParameter(int[,] grid, int i, int j)
    		{
    			int result = 0;
    			if (i - 1 < 0)
    			{
    				result += 1;
    			}
    			else if(grid[i-1, j] == 0)
    			{
    				result += 1;
    			}
    
    			if (j - 1 < 0)
    			{
    				result += 1;
    			}
    			else if (grid[i, j-1] == 0)
    			{
    				result += 1;
    			}
    
    			if (i+1 >= grid.GetLength(0))
    			{
    				result += 1;
    			}
    			else if (grid[i+1, j] == 0)
    			{
    				result += 1;
    			}
    
    			if (j + 1 >= grid.GetLength(1))
    			{
    				result += 1;
    			}
    			else if (grid[i, j + 1] == 0)
    			{
    				result += 1;
    			}
    
    			return result;
    		}
    

Log in to reply
 

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