Python beats 95+% (noob)


  • 0
    W

    class Solution(object):
    def islandPerimeter(self, grid):
    """
    :type grid: List[List[int]]
    :rtype: int
    """
    y_index = 0
    total = 0
    cover = 0

        for y in grid:
            y_index += 1
            x_index = 0
            for x in y:
                x_index += 1
                if x == 1:
                    total +=1
                    if len(grid) > y_index and grid[y_index][x_index-1] == 1:
                        cover += 1
                    if len(y) > x_index and grid[y_index-1][x_index] == 1:
                        cover +=1
        
        return total*4-cover*2

  • 0
    J

    @wuchao_china Thank you for sharing this. Can you explain it? I don't understand why you return total4-cover2


  • 0
    H

    Could you explain what is y,x and y_index and x_index?


Log in to reply
 

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