Python solution


  • 0
    P
    class Solution(object):
      def islandPerimeter(self, grid):
        r = len(grid)
        c = len(grid[0])
        
        f = [[0 for i in range(c+2)] for j in range(r+2)]
        
        for i in range(len(f[0])) :
            f[0][i] = 0
            f[r+1][i] = 0 
        
        for i in range(len(f)) :
            f[i][0] = 0
            f[i][c+1] = 0
        
        for i in range(1, len(f)-1) :
            for j in range(1, len(f[0])-1) :
                f[i][j] = grid[i-1][j-1]
            
        per = 0
        
        for i in range(1, len(f)-1) :
            for j in range(1, len(f[0])-1) :
                if f[i][j] :
                    per += (f[i-1][j] == 0) + (f[i+1][j] == 0) + (f[i][j-1] == 0) + (f[i][j+1] == 0)
        return per

Log in to reply
 

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