python leetcode 463. Island Perimeter

每个1有四条边count+=4,如果有重合则减去两条边count-=2

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

猜你喜欢

转载自blog.csdn.net/Neekity/article/details/84778499