题目如下:

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

代码如下:

  1. class Solution(object):
  2. def surfaceArea(self, grid):
  3. """
  4. :type grid: List[List[int]]
  5. :rtype: int
  6. """
  7. res = 0
  8. for i in range(len(grid)):
  9. for j in range(len(grid[i])):
  10. if grid[i][j] == 0:
  11. continue
  12. area = 2 + 4 * grid[i][j]
  13. if i-1 >= 0:
  14. area -= min(grid[i-1][j],grid[i][j])
  15. if j-1 >= 0:
  16. area -= min(grid[i][j],grid[i][j-1])
  17. if i + 1 < len(grid):
  18. area -= min(grid[i][j],grid[i+1][j])
  19. if j + 1 < len(grid[i]):
  20. area -= min(grid[i][j],grid[i][j+1])
  21. res += area
  22. 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_easy】892. Surface Area of 3D Shapes

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

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

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

  4. 【leetcode】883. Projection Area of 3D Shapes

    题目如下: 解题思路:分别求出所有立方体的个数,各行的最大值之和,各列的最大值之和.三者相加即为答案. 代码如下: class Solution(object): def projectionArea ...

  5. [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 ...

  6. 892. Surface Area of 3D Shapes

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

  7. 【Leetcode_easy】883. Projection Area of 3D Shapes

    problem 883. Projection Area of 3D Shapes 参考 1. Leetcode_easy_883. Projection Area of 3D Shapes; 完

  8. 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 ...

  9. [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 ...

随机推荐

  1. python中将12345转换为54321

    #将12345转换为54321 a = 12345789 ret = 0 #当a不为零的时候,循环条件为true,执行语句块 while a : #对a求余数,第一次循环则把5求出来 last = a ...

  2. Win32下session和window station以及desktop一些介绍和应用

    会话(session).工作站(WindowStation).桌面(Disktop).窗口(window) https://blog.csdn.net/hlswd/article/details/77 ...

  3. thread_process_action

    import math import random import re import sys import threading from time import ctime, sleep from l ...

  4. Vagrant 入门 - 清理(teardown)

    原文地址 我们现在有一个功能齐全的虚拟机,可以用于基本 Web 开发.但如果现在需要更换设备,或者在另一个项目上工作,如何清理我们的开发环境? 借助 Vagrant,可以暂停(suspend),停止( ...

  5. NYOJ 654喜欢玩warcraft的ltl(01背包/常数级优化)

    传送门 Description ltl 非常喜欢玩warcraft,因为warcraft十分讲究团队整体实力,而他自己现在也为升级而不拖累团队而努力. 他现在有很多个地点来选择去刷怪升级,但是在每一个 ...

  6. Linux——文件打包与压缩

    Linux 下常见常用的压缩包文件格式有*.zip,*.rar,*.7z*.gz,*.xz,*.bz2,*.tar,*.tar.gz,*.tar.xz,*tar.bz2等后缀的压缩文件 文件后缀名 说 ...

  7. ELK+Filebeat (1)

    1 Filebeat介绍 Filebeat是Beat成员之一,基于Go语言,无任何依赖,并且比logstash更加轻量,非常适合安装在生产机器上,不会带来过高的资源占用,轻量意味着简单,所以Fileb ...

  8. js 解决函数加载的问题

    var queue = function(funcs, scope) {         (function next() {               if(funcs.length > 0 ...

  9. 两台centos,用yum install 安装,一台成功,一台失败

    记录一下问题: 两台centos,用yum install 安装软件,一台成功,一台失败 第一步:查看yum源  yum repolist enabled 疑问:centos安装的方法一致,但yum源 ...

  10. LeetCode #807. Max Increase to Keep City Skyline 保持城市天际线

    https://leetcode-cn.com/problems/max-increase-to-keep-city-skyline/ 执行用时 : 3 ms, 在Max Increase to Ke ...