在 N * N 的网格上,我们放置一些 1 * 1 * 1  的立方体。

每个值 v = grid[i][j] 表示 v 个正方体叠放在单元格 (i, j) 上。

返回结果形体的总表面积。

示例 1:

输入:[[2]] 输出:10

示例 2:

输入:[[1,2],[3,4]] 输出:34

示例 3:

输入:[[1,0],[0,2]] 输出:16

示例 4:

输入:[[1,1,1],[1,0,1],[1,1,1]] 输出:32

示例 5:

输入:[[2,2,2],[2,1,2],[2,2,2]] 输出:46

提示:

  • 1 <= N <= 50
  • 0 <= grid[i][j] <= 50

方块个数 * 6 减去再被遮住的面积。

class Solution {
public:
int surfaceArea(vector<vector<int> >& grid) {
int sumArea = 0;
int r = grid.size();
int c = grid[0].size();
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
if(grid[i][j] == 0)
continue;
int temp = grid[i][j] * 6;
temp = temp - (grid[i][j] - 1) * 2;
if(i - 1 >= 0)
{
temp = temp - min(grid[i - 1][j], grid[i][j]);
}
if(i + 1 < r)
{
temp = temp - min(grid[i + 1][j], grid[i][j]);
}
if(j - 1 >= 0)
{
temp = temp - min(grid[i][j - 1], grid[i][j]);
}
if(j + 1 < c)
{
temp = temp - min(grid[i][j + 1], grid[i][j]);
}
sumArea += temp;
}
}
return sumArea;
}
};

Leetcode892.Surface Area of 3D Shapes三维形体的表面积的更多相关文章

  1. Leetcode883.Projection Area of 3D Shapes三维形体投影面积

    在 N * N 的网格中,我们放置了一些与 x,y,z 三轴对齐的 1 * 1 * 1 立方体. 每个值 v = grid[i][j] 表示 v 个正方体叠放在单元格 (i, j) 上. 现在,我们查 ...

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

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

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

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

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

  5. 892. Surface Area of 3D Shapes

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

  6. [Swift]LeetCode892. 三维形体的表面积 | 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. C#LeetCode刷题之#892-三维形体的表面积(Surface Area of 3D Shapes)

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

  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. 网络结构解读之inception系列二:GoogLeNet(Inception V1)

    网络结构解读之inception系列二:GoogLeNet(Inception V1) inception系列的开山之作,有网络结构设计的初期思考. Going deeper with convolu ...

  2. [转]WPF 构建无外观(Lookless)控件

    构建一个用户可以使用Template属性设置外观的WPF控件需要以下几步 1.继承自System.Windows.Controls.Control 2.设置DefaultStyleKeyPropert ...

  3. 搭建Angular环境

    一.安装nodejs 登录nodejs官网,下载对应系统版本:安装,只要一直下一步即可. 在nodejs中自带了npm,不需要独立安装npm. 输入node -v /npm -v  查看node和np ...

  4. linux下mysql的配置问题

    设置MySQL 复制以下配置信息到新建的my.ini(windows下的文件)文件中. [mysqld] # 设置3306端口 port= # 设置mysql的安装目录 basedir=D:\Prog ...

  5. Redis学习笔记02-消息队列与延时队列

    写在前面:Redis的消息队列并不是专业的消息队列,没有ACK保证,没有特别多的高级特性,如果对消息的可靠性有很高的要求,就放弃它吧. 1.Redis消息队列 Redis通过内部的list数据结构来实 ...

  6. MFC 双缓存绘图

    在SDI应用程序中,当我们需要时刻动态刷新界面的时候,如果我们一直使用,UpdateAllView()那么就会出现屏幕不停闪烁.闪屏非常严重,特别是一直在动态刷新的时候.并且在闪屏的过程中 我们根本就 ...

  7. MapReduce:详解Shuffle(copy,sort,merge)过程(转)

    Shuffle过程是MapReduce的核心,也被称为奇迹发生的地方.要想理解MapReduce, Shuffle是必须要了解的.我看过很多相关的资料,但每次看完都云里雾里的绕着,很难理清大致的逻辑, ...

  8. spotbus gradle-qulity-plugiin 多项目bug检查

    https://spotbugs.readthedocs.io/en/latest/bugDescriptions.html https://xvik.github.io/gradle-quality ...

  9. TZ_09_MyBatis的pageHelper

    1.分页操作使用MyBatis的PageHelper 1>导入pageHelper的坐标 <dependency> <groupId>com.github.pagehel ...

  10. android搭建

    搭建:https://www.cnblogs.com/zoupeiyang/p/4034517.html#1 android sdk manager 翻墙:http://www.androiddevt ...