脚本宝典收集整理的这篇文章主要介绍了Leetcode PHP题解--D65 892. Surface Area of 3D Shapes,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
D65 892. Surface Area of 3D Shapes
题目链接
892. Surface Area of 3D Shapes
题目分析
思路
三维数组中,$arr[$i][$j]
的值表示在该点上柱状体的高度。
因此,对每一个值,需要算做6份(1*1*1)的面积。但是,当上方或下方有块时,需要减去相应面积。
当相邻位置有方块时,需要减去相应表面积。当前柱体和相邻柱体都需要减去。但只减去两个柱体中,较矮的柱体的高度*2。
要记住,在两个方向上都需要做该判断。
最终代码
<?php
class Solution {
/**
* @param Integer[][] $grid
* @return Integer
*/
function surfaceArea@H_777_52@($grid) {
$total = 0;
$ys = count($grid);
for($i = 0; $i<$ys; $i++){
$xs = count($grid[$i]);
for($j = 0; $j<$xs; $j++){
$total += $grid[$i][$j]*4+2*($grid[$i][$j]!=0);
if($i+1<$ys){
$total -= min($grid[$i][$j],$grid[$i+1][$j])*2;
}
if($j+1<$ys){
$total -= min($grid[$i][$j],$grid[$i][$j+1])*2;
}
}
}
return $total;
}
}
以上是脚本宝典为你收集整理的Leetcode PHP题解--D65 892. Surface Area of 3D Shapes全部内容,希望文章能够帮你解决Leetcode PHP题解--D65 892. Surface Area of 3D Shapes所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。