内容简介:给定一个二维数组,代表一个二维表格。里面包含
463. Island Perimeter
题目链接
题目分析
给定一个二维数组,代表一个二维表格。
里面包含 0
和 1
两种数字。
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)资助。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Where Wizards Stay Up Late
Katie Hafner / Simon & Schuster / 1998-1-21 / USD 16.00
Twenty five years ago, it didn't exist. Today, twenty million people worldwide are surfing the Net. "Where Wizards Stay Up Late" is the exciting story of the pioneers responsible for creating the most......一起来看看 《Where Wizards Stay Up Late》 这本书的介绍吧!