原题

原题链接

You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells). The island doesn't have "lakes" (water inside that isn't connected to the water around the island). One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.

Example:

[[0,1,0,0],

[1,1,1,0],

[0,1,0,0],

[1,1,0,0]]

Answer: 16

Explanation: The perimeter is the 16 yellow stripes in the image below:

给定一个二维整数的方格地图,1代表陆地,0代表有水的地方。每个方格都是垂直或者水平连接的,没有斜线。被水包围的中间有一个小岛(小岛可能包含了很多个的陆地方格),小岛里没有岛中湖(内部的水和外部的水是连通的)。每个方格单元的边长都是1。各个地方都是成直角的,长度宽度不超过100,判断小岛的边长。

思路

判断其四面是否有陆地,四面都有陆地的对周长无贡献,两面有陆地的贡献周长为2,一面有陆地的贡献周长为3,四面都没有陆地的贡献周长为4。

代码

class Solution {
public:
int islandPerimeter(vector<vector<int>>& grid) {
int sum=0;
int m=grid.size(),n=grid[0].size();
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
if(grid[i][j]){
if(i==0||grid[i-1][j]==0) sum++;
if(i==m-1||grid[i+1][j]==0) sum++;
if(j==0||grid[i][j-1]==0) sum++;
if(j==n-1||grid[i][j+1]==0) sum++;
}
return sum;
}
};

LeetCode - 463. Island Perimeter - O(MN)- (C++) - 解题报告的更多相关文章

  1. LeetCode 463 Island Perimeter 解题报告

    题目要求 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 rep ...

  2. LeetCode 463. Island Perimeter (岛的周长)

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...

  3. [LeetCode] 463. Island Perimeter 岛的周长

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...

  4. LeetCode 463. Island Perimeter岛屿的周长 (C++)

    题目: You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 repr ...

  5. LeetCode: 463 Island Perimeter(easy)

    题目: You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 repr ...

  6. 3. leetcode 463 Island Perimeter

    思路:设原始周长为4*节点数,每当出现一次相邻的情况,原始周长会减2.

  7. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  8. 463. Island Perimeter - LeetCode

    Question 463. Island Perimeter Solution 题目大意:给出一个二维数组1表示陆地0表示海,求陆地的周长 思路: 重新构造一张地图grid2即一个二维数组,比原数组大 ...

  9. 【LeetCode】697. Degree of an Array 解题报告

    [LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...

随机推荐

  1. Knowledge Point 20180305 十进制转换成二进制浮点数

    如何将十进制的浮点数 转换二进制的浮点数,分为两部分: 1. 先将整数部分转换为二进制, 2. 将小数部分转换为二进制, 然后将整数部分与小数部分相加. 以 20.5 转换为例,20转换后变为1010 ...

  2. Unity 游戏框架搭建 (十七) 静态扩展GameObject实现链式编程

    本篇本来是作为原来 优雅的QChain的第一篇的内容,但是QChain流产了,所以收录到了游戏框架搭建系列.本篇介绍如何实现GameObject的链式编程. 链式编程的实现技术之一是C#的静态扩展.静 ...

  3. 霍金:AI或许能根除疾病和贫穷,但也可能摧毁人类 | GMIC 2017

    在我的一生中,我见证了社会深刻的变化.其中最深刻的,同时也是对人类影响与日俱增的变化,是人工智能的崛起.简单来说,我认为强大的人工智能的崛起,要么是人类历史上最好的事,要么是最糟的.我不得不说,是好是 ...

  4. redhat6 快速部署percona

    1.首先得能访问外网 2.yum install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-relea ...

  5. Sass使用

    1. 什么是Sass ???   Sass是一个将脚本解析成CSS的脚本语言,也可以称为CSS扩展语言. 2. 安装: 第一步:先安装Ruby (sass基于Ruby语言开发而成,因此安装sass前需 ...

  6. Centos7 安装ipython 和 ipython3

    [root@localhost ~]# wget https://pypi.python.org/packages/79/63/b671fc2bf0051739e87a7478a207bbeb45cf ...

  7. CentOS7 LNMP+phpmyadmin环境搭建(一、虚拟机及centos7安装)

    前一阵子配公司的服务器的时候,发现网上好多教程杂乱无章,然后便根据网上已有资料自己整理了一个lnmp环境的安装教程.因为懒,已经好久没写过博客了.趁着这次公司招新人,把之前整理的文档又整理了一次,顺便 ...

  8. 【NXP开发板应用—智能插排】2.初步解析example之GPI

    [前言] 首先感谢深圳市米尔科技有限公司举办的这次活动并予以本人参加这次活动的机会,以往接触过嵌入式,但那都是皮毛,最多刷个系统之类的,可以说对于嵌入式系统开发这件事情是相当非常陌生的,这次活动为我提 ...

  9. 关于mysql 8.0.13zip包安装

    mysql 8.0.13默认有一个data文件夹,这个文件夹得删了,不然安装服务时候会有日志文件提示报错: Failed to find valid data directory. Data Dict ...

  10. redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect time out

    redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect ti ...