892.leetcode题目讲解(Python):三维形体的表面积(Surface Area of 3D Shapes)

题目

892.leetcode题目讲解(Python):三维形体的表面积(Surface Area of 3D Shapes)_第1张图片
题目

解题思路

解这道题的思路为,首先获取一个坐标点长方体的表面积,计算公式如下:

surface = grid[i][j] * 4 + 2

然后减去其 “上、下、左、右” 与相邻长方体的重叠表面积,减去的面积应该是相邻两个长方体中高度(grid[i][j])较小的那个。

参考代码

代码一:为了方便理解,我们先写个不太精炼的, 如下:

'''
@auther: Jedi.L
@Date: Wed, Mar 27, 2019 12:44
@Email: [email protected]
@Blog: www.tundrazone.com
'''

class Solution:
    def surfaceArea(self, grid):
        self.grid = grid
        tsa = 0  # total surface area of resulting shapes
        x = len(self.grid) - 1  # max i for grid[i][j]
        y = len((self.grid[0])) - 1  # max j for grid[i][j]
        i = 0
        while i <= x:
            j = 0
            while j <= y:
                if self.grid[i][j] != 0:
                    tsa += self.grid[i][j] * 4 + 2  # surface are at point
                    # we count the left surface area of this point as follow:
                    # up close
                    if i-1 >= 0 and self.grid[i-1][j] >= self.grid[i][j]:
                        tsa = tsa - self.grid[i][j]
                    elif i-1 >= 0 and self.grid[i-1][j] < self.grid[i][j]:
                        tsa = tsa - self.grid[i-1][j]
                    # down close
                    if i+1 <= x and self.grid[i+1][j] >= self.grid[i][j]:
                        tsa = tsa - self.grid[i][j]
                    elif i+1 <= x and self.grid[i+1][j] < self.grid[i][j]:
                        tsa = tsa - self.grid[i+1][j]
                    # left close
                    if j-1 >= 0 and self.grid[i][j-1] >= self.grid[i][j]:
                        tsa = tsa - self.grid[i][j]
                    elif j-1 >= 0 and self.grid[i][j-1] < self.grid[i][j]:
                        tsa = tsa - self.grid[i][j-1]
                    # right close
                    if j + 1 <= y and self.grid[i][j+1] >= self.grid[i][j]:
                        tsa = tsa - self.grid[i][j]
                    elif j + 1 <= y and self.grid[i][j+1] < self.grid[i][j]:
                        tsa = tsa - self.grid[i][j+1]
                j = j + 1
            i = i + 1
        return tsa

代码二:仔细观察代码一,我们会发现其中的一些代码没有必要,每个长方体只需要考虑自己的下方和右方相邻的长方体,然后减去2倍的重叠面积就可以了,精简后的代码如下:

'''
@auther: Jedi.L
@Date: Wed, Mar 27, 2019 12:44
@Email: [email protected]
@Blog: www.tundrazone.com
'''

class Solution:
    def surfaceArea(self, grid):
        self.grid = grid
        tsa = 0  # total surface area of resulting shapes
        x = len(self.grid) - 1  # max i for grid[i][j]
        y = len((self.grid[0])) - 1  # max j for grid[i][j]
        i = 0
        while i <= x:
            j = 0
            while j <= y:
                if self.grid[i][j] != 0:
                    tsa += self.grid[i][j] * 4 + 2  # surface are at point
                    # we count the left surface area of this point as follow:
                    # down close
                    if i + 1 <= x and self.grid[i + 1][j] >= self.grid[i][j]:
                        tsa = tsa - 2*self.grid[i][j]
                    elif i + 1 <= x and self.grid[i + 1][j] < self.grid[i][j]:
                        tsa = tsa - 2*self.grid[i + 1][j]
                    # right close
                    if j + 1 <= y and self.grid[i][j + 1] >= self.grid[i][j]:
                        tsa = tsa - 2*self.grid[i][j]
                    elif j + 1 <= y and self.grid[i][j + 1] < self.grid[i][j]:
                        tsa = tsa - 2*self.grid[i][j + 1]
                j = j + 1
            i = i + 1
        return tsa

源码地址:
https://github.com/jediL/LeetCodeByPython

其它题目:[leetcode题目答案讲解汇总(Python版 持续更新)]
(https://www.jianshu.com/p/60b5241ca28e)

ps:如果您有好的建议,欢迎交流 :-D,
也欢迎访问我的个人博客 苔原带 (www.tundrazone.com)

你可能感兴趣的:(892.leetcode题目讲解(Python):三维形体的表面积(Surface Area of 3D Shapes))