easy understand python solution


  • 0

    '''
    class Solution(object):
    def islandPerimeter(self, grid):
    """
    :type grid: List[List[int]]
    :rtype: int
    """
    i, j = 0, 0
    sum = 0
    if grid[0][0]:
    sum += 4

        for i in range(1,len(grid)):
            if grid[i][0]:
                if grid[i-1][0]:
                    sum += 2
                else:
                    sum += 4
        
        for j in range(1,len(grid[0])):
            if grid[0][j]:
                if grid[0][j-1]:
                    sum += 2
                else:
                    sum += 4
            
        for i in range(1,len(grid)):
            for j in range(1, len(grid[0])):
                if grid[i][j]:
                    if grid[i-1][j] and grid[i][j-1]:
                        #  sum += 2-2 two sides inside of the intersection
                        pass
                    elif grid[i-1][j] or grid[i][j-1]:
                        # one side inside the intersection
                        sum += 2
                    else:
                        sum += 4
        return sum
    

    '''


Log in to reply
 

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