题目:

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. Beta阶段第四次冲刺

    Beta阶段第四次冲刺 严格按照Git标准来,组员有上传Git的才有贡献分没有的为0 代码签入图 1.part1 -站立式会议照片 2.part2 -项目燃尽图 3.part3 -项目进展 1.正在进 ...

  2. 团队作业7——第二次项目冲刺(Beta版本)day1

    项目成员:  曾海明(组长):201421122036 于波(组员):201421122058 蓝朝浩(组员):201421122048 王珏 (组员):201421122057 叶赐红(组员):20 ...

  3. 配置好jdk后,cmd编写java -version还是报找不到jdk

    找了好长时间没查出问题在哪,配置的都没有问题,最后搜了搜网上发现,jdk的这两行得在最上面才行.

  4. windows下的MySql实现读写分离

    MySql读写分离 1.删除系统服务 sc delete 服务名 2.复制安装好的3380文件夹到3381 3.进入3381\logs目录下将所有文件删除 4.进入3381\data目录,将所有的lo ...

  5. JS强制刷新页面、清除缓存刷新

    清理网站缓存的几种方法 meta方法 <meta http-equiv="pragma" content="no-cache"> <meta ...

  6. Dig命令解析结果

    dig -t RT NAME @NS -t RT 指定要查询的资源记录类型 NAME 需要解析的域(域名) @NS 指定那个域名服务器负责解析 [root@xss ~]# dig www.ihoney ...

  7. 【转】在嵌入式Linux和PC机Linux下使用popen函数时,程序运行结果有差异。

    下面程序演示了在嵌入式Linux和PC机Linux下使用popen函数时,程序的运行结果是有差异的. 两个程序 atest.c 和 btest.c,atest 检查是否有 btest 进程运行,如果没 ...

  8. Android MD5算法

    package com.example.myapi.md5; import java.io.UnsupportedEncodingException; import java.security.Mes ...

  9. jqgrid 配置行号及行号的宽度

    有时,我们想把jqgrid的行号按指定的宽度显示出来,如何实现? 通过 rownumbers:true  设置启用行号 通过 rownumWidth 配置行号列的宽度 $("#jqGrid& ...

  10. 没事做的Delphi版的俄罗斯方块游戏Demo

    源代码下载