呓语Beta 2.0 首页 镜头后 灶台前 捐赠者名单 有趣的小站

leetcode 695 Max Area Of Island

文档信息

leetcode 695 Max Area Of Island

解法一

class Solution(object):
    def maxAreaOfIsland(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        seen = set()
        
        def area(r, c):
            if not ( 0 <= r < len(grid) and 0 <= c < len(grid[0])
                   and (r, c) not in seen and grid[r][c]):
                return 0
            seen.add((r, c))
            return (1 + area(r+1, c) + area(r-1, c) +
                       area(r, c+1) + area(r, c-1))
        
        return max(area(r, c)
                  for r in range(len(grid))
                  for c in range(len(grid[0])))

note

捐赠支持

可以使用支付宝转帐给richard.ma.19850509#gmail.com(请将#替换为@),谢谢!

Fork me on GitHub