leetcode-892 三维形体的表面积 Python

class Solution:
    def surfaceArea(self, grid: List[List[int]]) -> int:
        def neighbours(i, j):
            overlap = 0
            for di, dj in ((1, 0), (-1, 0), (0, 1), (0, -1)):
                if 0<= i + di < len(grid) and 0 <= j +dj < len(grid[0]):
                    overlap += min(grid[i][j], grid[i + di][j + dj])
            return overlap


        ans = 0
        for i in range(len(grid)):
            for j in range(len(grid[0])):
                if grid[i][j] != 0:
                    ans += 4 * grid[i][j] + 2 - neighbours(i, j)
        return ans





发布了51 篇原创文章 · 获赞 0 · 访问量 1006
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 黑客帝国 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览