clear python


  • 0
    Q
    class Solution(object):
        def islandPerimeter(self, grid):
            """
            :type grid: List[List[int]]
            :rtype: int
            """
            adj_lt = list()
            for i, v1 in enumerate(grid):
                for j, v2 in enumerate(grid[i]):
                    if v2 == 1:
                        adj_lt.append(self.adj_nums(grid, i, j))
            
            return sum(adj_lt)
            
        def adj_nums(self, grid, i, j):
            row_lim = len(grid) - 1
            col_lim = len(grid[0]) - 1
            up_v = grid[i-1][j] if 0 <= (i-1) else 0
            dn_v = grid[i+1][j] if (i+1) <= row_lim else 0
            lt_v = grid[i][j-1] if 0 <= (j-1) else 0
            rt_v = grid[i][j+1] if (j+1) <= col_lim else 0
            
            adj_sum = up_v + dn_v + lt_v + rt_v
            return 4 - adj_sum
    

Log in to reply
 

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