Island Perimeter

每一个点对四边扫描一下就可以,看看是不是有相邻1,没有就可以加一

class Solution(object):
    def islandPerimeter(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        n = len(grid)
        if not n:
            return 0

        m = len(grid[0])
        if not m:
            return 0

        result = 0
        for i in xrange(n):
            for j in xrange(m):
                if grid[i][j] == 1:
                    if i - 1 < 0 or grid[i - 1][j] == 0:
                        result += 1
                    if i + 1 >= n or grid[i + 1][j] == 0:
                        result += 1
                    if j - 1 < 0 or grid[i][j - 1] == 0:
                        result += 1
                    if j + 1 >= m or grid[i][j + 1] == 0:
                        result += 1

        return result

results matching ""

    No results matching ""