Python solution easy to understand


  • 0
    class Solution(object):
        def islandPerimeter(self, grid):
            """
            :type grid: List[List[int]]
            :rtype: int
            """
            perometer = 0
            up, left, right, down = [0, 0, 0, 0]
            bounding = 0
            for i in xrange(len(grid)):
                for j in xrange(len(grid[i])):
                    if grid[i][j]==0:
                        if i==0:
                            up = 0
                            if i+1 <= len(grid)-1:
                                down = grid[i+1][j]
                            if j==0:
                                left = 0
                                if j+1 <= len(grid[i])-1:
                                    right = grid[i][j+1]
                            elif j==(len(grid[i])-1):
                                right = 0
                                left = grid[i][j-1]
                            else:
                                left = grid[i][j-1]
                                right = grid[i][j+1]
                        elif i==(len(grid)-1):
                            down = 0
                            up = grid[i-1][j]
                            if j==0:
                                left = 0
                                if j+1 <= len(grid[i])-1:
                                    right = grid[i][j+1]
                            elif j==(len(grid[i])-1):
                                right = 0
                                left = grid[i][j-1]
                            else:
                                left = grid[i][j-1]
                                right = grid[i][j+1]
                        else:
                            down = grid[i+1][j]
                            up = grid[i-1][j]
                            if j==0:
                                left = 0
                                if j+1 <= len(grid[i])-1:
                                    right = grid[i][j+1]
                            elif j==(len(grid[i])-1):
                                right = 0
                                left = grid[i][j-1]
                            else:
                                left = grid[i][j-1]
                                right = grid[i][j+1]
                        perometer += up + down + left + right
                    elif grid[i][j]==1:
                        if i==0:
                            bounding += 1
                        if i==(len(grid)-1):
                            bounding += 1
                        if j==0:
                            bounding += 1
                        if j==(len(grid[i])-1):
                            bounding += 1
            perometer += bounding
            
            return perometer
    
    1. count the number of 1 around 0, and the direction is just down the row or col, then get the perometer;

    2. count the number of 1 which is in the boundary, get the bounding;

    3. add perometer += bounding as the result;


Log in to reply
 

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