Leetcode-Easy 887. Projection Area of 3D Shapes

题目描述

给出NN的网格grid,然后根据grid[i][j]的值在上面放置相应数量的11*1的立方体,然后求在xy,xz,yz三个平面投射的总面积

Leetcode-Easy 887. Projection Area of 3D Shapes_第1张图片

思路

当时自己没有想到好办法,就是按部就班的分别求三个面的面积,注意求xy的面积的时候需要考虑grid[i][j]值是否为0

代码实现

class Solution:
    def projectionArea(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        # xy平面的面积
        size=len(grid)
        xy_area=0
        for i in range(size):
            for j in range(size):
                if grid[i][j]>0:
                    xy_area+=1
        # xz平面的面积
        xz_area=0
        temp_column=[]
        for i in range(size):
            for row in grid:
                temp_column.append(row[i])
            xz_area+=max(temp_column)
            temp_column=[]
            
        # yz平面的面积
        yz_area=0
        for i in range(size):
            yz_area+=max(grid[i])
            
        return xy_area+xz_area+yz_area            

你可能感兴趣的:(Leetcode-Easy 887. Projection Area of 3D Shapes)