5 lines python beats 92%


  • 0
    S
    class Solution(object):
        def islandPerimeter(self, grid):
            """
            :type grid: List[List[int]]
            :rtype: int
            """
            sum,x,y = 0,len(grid),len(grid[0])
            for i,row in enumerate(grid):
                for j,item in enumerate(row):
                    sum += 4 - 2*((grid[i + 1][j] if i < x - 1 else 0) + (grid[i][j + 1] if j < y - 1 else 0)) if item else 0
            return sum
    

Log in to reply
 

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