Beats 95% of Python solutions...


  • 0
    M

    Very simple and calls less if conditions:

    class Solution(object):
        def islandPerimeter(self, grid):
            """
            :type grid: List[List[int]]
            :rtype: int
            """
            perim = 0
            for i in range(len(grid)):
                for j in range(len(grid[0])):
                    if grid[i][j] == 1:
                        perim += 4
                        if (i>=1) and grid[i-1][j] == 1:
                            perim -= 2
                        if (j>=1) and grid[i][j-1] == 1:
                            perim -= 2
            return perim
    

  • 0
    L

    cool! It was one of the most simple python solution I've seen.By the way, do you konw the time and space complexity? Is O(n^2) ?


Log in to reply
 

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