very easy to understand solution


  • 0
    P

    very easy to understand solution
    python

    class Solution(object):
        def islandPerimeter(self, grid):
            """
            :type grid: List[List[int]]
            :rtype: int
            """
            all = 0
            for ii in range(len(grid)):
                for jj in range(len(grid[0])):
                    if grid[ii][jj] == 0:
                        continue
                    if grid[ii][jj] == 1:
                        all += 4
                    if ii-1 >= 0 and grid[ii-1][jj] == 1:
                        all -= 1
                    if ii+1 < len(grid) and grid[ii+1][jj] == 1:
                        all -= 1
                    if jj+1 < len(grid[0]) and grid[ii][jj+1] == 1:
                        all -= 1
                    if jj-1 >= 0 and grid[ii][jj-1] == 1:
                        all -= 1
            return all
    

Log in to reply
 

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