Easy to read Python solution


  • 10
    class Solution(object):
        def islandPerimeter(self, grid):
            """
            :type grid: List[List[int]]
            :rtype: int
            """
            if not grid:
                return 0
    
            def sum_adjacent(i, j):
                adjacent = (i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1),
                res = 0
                for x, y in adjacent:
                    if x < 0 or y < 0 or x == len(grid) or y == len(grid[0]) or grid[x][y] == 0:
                        res += 1
                return res
    
            count = 0
            for i in range(len(grid)):
                for j in range(len(grid[0])):
                    if grid[i][j] == 1:
                        count += sum_adjacent(i, j)
            return count
    

  • 0
    I

    @Uduse This is nice use of tuples. I had the same idea but couldn't make it concise like yours. Thanks for sharing.


Log in to reply
 

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