Leetcode PHP题解--D38 463. Island Perimeter

栏目: PHP · 发布时间: 7年前

内容简介:给定一个二维数组,代表一个二维表格。里面包含

463. Island Perimeter

题目链接

463. Island Perimeter

题目分析

给定一个二维数组,代表一个二维表格。

里面包含 01 两种数字。

1代表有内容,0代表没有。

计算有内容的格子的周长。

思路

最简单的办法是,判断当前格子是否位1,且上下左右是否为0。

当任意一个方向有0时,那个方向计算有一个边长。

当都为0时,即当前位置是单独的一个格子,算上下左右共4条边。

最终代码

<?php
class Solution {
    function islandPerimeter($grid) {
            $edges = 0;
                    $width = count($grid[0]);
                            $height = count($grid);
                                    foreach($grid as $y=>$row){
                                                foreach($row as $x=>$col){
                                                                if($col==0){
                                                                                    continue;
                                                                                                    }
                                                                                                                    if(!isset($grid[$y][$x+1])||$grid[$y][$x+1]==0){
                                                                                                                                        $edges++;
                                                                                                                                                        }
                                                                                                                                                                        if(!isset($grid[$y][$x-1])||$grid[$y][$x-1]==0){
                                                                                                                                                                                            $edges++;
                                                                                                                                                                                                            }
                                                                                                                                                                                                                            if(!isset($grid[$y+1][$x])||$grid[$y+1][$x]==0){
                                                                                                                                                                                                                                                $edges++;
                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                if(!isset($grid[$y-1][$x])||$grid[$y-1][$x]==0){
                                                                                                                                                                                                                                                                                                    $edges++;
                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                return $edges;
                                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                                    }
若觉得本文章对你有用,欢迎用[爱发电](https://afdian.net/@skys215)资助。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Speed Up Your Site

Speed Up Your Site

Andrew B. King / New Riders Press / 2003-01-14 / USD 39.99

There's a time bomb on the web: user patience. It starts ticking each time someone opens one of your pages. You only have a few seconds to get compelling content onto the screen. Fail, and you can kis......一起来看看 《Speed Up Your Site》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具