题目要求

On a N * N grid, we place some 1 * 1 * 1 cubes.

Each value v = grid[i][j] represents a tower of v cubes placed on top of grid cell (i, j).

Return the total surface area of the resulting shapes.

题目分析及思路

给定一个N*N网格,在其上放置1*1*1的小方块,二维数组的值即为当前位置的高度,要求返回这样一个3D图形的表面积。可以遍历数组的每一个元素,若元素不为零,则可确定其上下两个面积算入最终的结果。然后对该位置的前后左右进行遍历,若存在元素,则若小于原元素,则返回它们的差算入最后结果,否则就不算。

python代码

class Solution:

def surfaceArea(self, grid: List[List[int]]) -> int:

N = len(grid)

res = 0

for r in range(N):

for c in range(N):

if grid[r][c]:

res += 2

for nr, nc in ((r-1,c), (r+1,c), (r,c-1), (r,c+1)):

if 0 <= nr < N and 0 <= nc < N:

nval = grid[nr][nc]

else:

nval = 0

res += max(grid[r][c] - nval, 0)

return res

LeetCode 892 Surface Area of 3D Shapes 解题报告的更多相关文章

  1. 【LeetCode】892. Surface Area of 3D Shapes 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. [LeetCode] 892. Surface Area of 3D Shapes 三维物体的表面积

    On a N * N grid, we place some 1 * 1 * 1 cubes. Each value v = grid[i][j] represents a tower of v cu ...

  3. LeetCode 883 Projection Area of 3D Shapes 解题报告

    题目要求 On a N * N grid, we place some 1 * 1 * 1 cubes that are axis-aligned with the x, y, and z axes. ...

  4. 【Leetcode_easy】892. Surface Area of 3D Shapes

    problem 892. Surface Area of 3D Shapes 题意:感觉不清楚立方体是如何堆积的,所以也不清楚立方体之间是如何combine的.. Essentially, compu ...

  5. 892. Surface Area of 3D Shapes

    问题 NxN个格子中,用1x1x1的立方体堆叠,grid[i][j]表示坐标格上堆叠的立方体个数,求这个3D多边形的表面积. Input: [[1,2],[3,4]] Output: 34 思路 只要 ...

  6. [LeetCode&Python] Problem 892. Surface Area of 3D Shapes

    On a N * N grid, we place some 1 * 1 * 1 cubes. Each value v = grid[i][j] represents a tower of v cu ...

  7. 【leetcode】892. Surface Area of 3D Shapes

    题目如下: 解题思路:对于v = grid[i][j],其表面积为s = 2 + v*4 .接下来只要在判断其相邻四个方向有没有放置立方体,有的话减去重合的面积即可. 代码如下: class Solu ...

  8. 【LeetCode】883. Projection Area of 3D Shapes 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学计算 日期 题目地址:https://leetc ...

  9. C#LeetCode刷题之#892-三维形体的表面积(Surface Area of 3D Shapes)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4136 访问. 在 N * N 的网格上,我们放置一些 1 * 1 ...

随机推荐

  1. [转]PowerDesigner大小写转换

    原文地址:https://blog.csdn.net/fzqlife/article/details/72769959?utm_source=blogxgwz7 在菜单栏找到:Tools-->E ...

  2. NodeJS的url验证库模块url-valid

    这是我10月份做的项目其中的一个部件,主要用于url检验的. 我们知道Javascript做url检验,通常是使用正则表达式来判定,其格式是否正确,例如: /^https?:\/\//.test(ur ...

  3. net use错误原因解决(精辟)(转)

      (1)"发生系统错误 1326. 登录失败: 未知的用户名或错误密码." 在远程机的"控制面板-文件夹选项-查看-简单的文件共享",去掉选取,然后再尝试连接 ...

  4. URLEncoder 和URLDecoder

    通常在字符串的编码转换上,可以使用这两个类: public static void main(String[] args) { String str = "你好吗?我很好!"; t ...

  5. Pwnium CTF2014 – MatterOfCombination writeup

    这道题是虽然只有75分,但是做出来的队伍却很少,我们队伍也没有做出来,这次是看到了0xAWES0ME 的解题思路后才有了这篇文章.原文地址可以点击看这里,英文的. 题目就是一张图片: 在网上可以找到这 ...

  6. spring 手动添加 bean 到容器,例子 :多数据源配置

    package com.thunisoft.spsjsb.config.db.decrypt; import com.alibaba.druid.pool.DruidDataSource; impor ...

  7. Please install [clang](http://clang.llvm.org/) or check configuration `clang.executable`

    解决方法: 1.安装clang 第一步:首先打开VScode编辑器 第二步:点击左侧“应用商店”栏 第三步:在“应用商店搜索拓展”栏输入关键字“clang” 第四步:安装提示的“C/c++ clang ...

  8. 向json中添加新的熟悉或对象 Add new attribute (element) to JSON object using JavaScript

    How do I add new attribute (element) to JSON object using JavaScript? JSON stands for JavaScript Obj ...

  9. [Object Tracking] How to learn Active contour model - Snake Model

    常见四种跟踪的思路: 区域:人体肢体识别.跟踪 模型:人体面部识别.跟踪 特征:摄像头3D定位 主动轮廓:(蛇模型属于这er,数学基础<图像处理的偏微分方程方法>,也是最流行的一个目前) ...

  10. [IR] Advanced XML Compression - XBW

    思考:与ISX对比后能得出什么结论 原理解析: We proposed the XBW-transform that mimics on trees the nice structural prope ...