python: first surround the island


  • 0
    E

    '''
    class Solution(object):
    def islandPerimeter(self, grid):

        per = 0
        #surround the input with water
        grid.insert(0,[0]*len(grid[0]))
        grid.insert(len(grid),[0]*len(grid[0]))
        for row in grid:
         row.insert(0,0)
            row.insert(len(row),0)
        #go explore the island
        for j,row in enumerate(grid[:]):
            for k,isle in enumerate(row[:]):
                if isle == 1:
                    if row[k-1]!=1:
                        per+=1
                    if row[k+1]!=1:
                        per+=1
                    if grid[j+1][k]!=1:
                        per+=1
                    if grid[j-1][k]!=1:
                        per+=1
    
        return per
    

    '''


Log in to reply
 

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