题目:

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:

Input:
[[0,1,0,0],
[1,1,1,0],
[0,1,0,0],
[1,1,0,0]] Output: 16 Explanation: The perimeter is the 16 yellow stripes in the image below:

分析:

给定一个二维数组,其中1表示岛屿,0表示水域,求所有岛屿的周长和。

这道题的关键也就是求出相邻岛屿,因为我们很好统计矩阵中1的个数,乘4再减去相邻岛屿重合的边就可以得到解。

在计算的时候我们可以发现一条重合的边,实际上是属于两个岛屿的,所以计算时要乘2,所以计算一个陆地周围是否有重合的边的时候,我们可以只计算上方和左边的两个方向,可以节省一定的时间。

程序:

class Solution {
public:
int islandPerimeter(vector<vector<int>>& grid) {
int res = ;
for(int i = ; i < grid.size(); ++i)
for(int j = ; j < grid[].size(); ++j){
if(grid[i][j]){
res += ;
if(i != && grid[i-][j])
res -= ;
if(j != && grid[i][j-])
res -= ;
}
}
return res;
}
};

LeetCode 463. Island Perimeter岛屿的周长 (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 represen ...

  2. 463 Island Perimeter 岛屿的周长

    详见:https://leetcode.com/problems/island-perimeter/description/ C++: class Solution { public: int isl ...

  3. LeetCode 463 Island Perimeter 解题报告

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

  4. Leetcode463.Island Perimeter岛屿的周长

    给定一个包含 0 和 1 的二维网格地图,其中 1 表示陆地 0 表示水域. 网格中的格子水平和垂直方向相连(对角线方向不相连).整个网格被水完全包围,但其中恰好有一个岛屿(或者说,一个或多个表示陆地 ...

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

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

  6. 463. Island Perimeter岛屿周长

    [抄题]: You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 re ...

  7. LeetCode - 463. Island Perimeter - O(MN)- (C++) - 解题报告

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

  8. 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 ...

  9. 3. leetcode 463 Island Perimeter

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

随机推荐

  1. linux 的常用命令---------第七阶段

       LVM 逻辑卷管理器  -----其作用为 :在线扩容 卷组 vG  (也叫LVM卷组) ------------------→     在此卷组vG上建立  :       逻辑卷组 LV ( ...

  2. oracle偏爱hostname

    记住: 只要是在使用oracle他家的产品,比如oracle database , weblogic :或者诸如此类,等等,等等,随便别的什么东西 首先要查改: /etc/hosts 127.0.0. ...

  3. QGIS里的编程模型

    项目(QgsProject) 用于读写项目状态 图层分组(QgsLayerTreeGroup) 项目树的分组节点,用来存放图层节点. 图层节点(QgsLayerTreeLayer) 项目树的图层节点. ...

  4. Python2.7-tempfile

    tempfile 模块,生成临时文件夹或文件,所生成的文件(夹)的名字都是随机的,但可以指定前缀.后缀和路径,中间由6位随机字符组成.应用程序经常要保存一些临时的信息,这些信息不是特别重要,没有必要写 ...

  5. java中线程的几种状态和停止线程的方法

    1.线程的状态图 需要注意的是:线程调用start方法是使得线程到达就绪状态而不是运行状态 2.停止线程的两种方法 1)自然停止:线程体自然执行完毕 2)外部干涉:通过线程体标识 1.线程类中定义线程 ...

  6. mysqldump: Got error: 1356 mysqldump的重要参数--force

    一个MySQL的备份突然变小了很多,但实际的数据量却一直在增长.备份脚本也没有调整过.为什么呢? 重现了一下备份过程,发现备份中遇到了如下错误: mysqldump: Got error: 1356: ...

  7. Can't create component 'xxx.xxx.xxx' as it has dependencies to be satisfied

    问题描述: Can't create component 'xxx.xxx.xxx' as it has dependencies to be satisfied. 问题原由: 没有对新建的实体映射类 ...

  8. WebHook之PHP实践@coding.net

    次写完代码, 打开FileZilla, 把写好的文件上传到vps上, 久而久之觉得腻烦, 寻思有没有更geek的方法, 便有此文. WebHook是跟随着Git而兴起的技术, 当你push到服务器的时 ...

  9. 详细解读Spark的数据分析引擎:Spark SQL

    一.spark SQL:类似于Hive,是一种数据分析引擎 什么是spark SQL? spark SQL只能处理结构化数据 底层依赖RDD,把sql语句转换成一个个RDD,运行在不同的worker上 ...

  10. python with原理

    在python2.5+中可以用with来保证关闭打开的文件 with open('hello.txt') as f: do some file operations 为什么要引入with呢? 在之前如 ...