题目:

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. NOIP2018考前抱佛脚——搜索复习

    目录 搜索 DFS 例1 P1101 单词方阵 题目描述 输入输出格式 输入输出样例 标程 例2 P1605 迷宫 题目背景 输入输出格式 输入输出样例 标程 例3 P1019 单词接龙 题目描述 输 ...

  2. Dijkstra(最短路求解)

    Dijkstra(最短路求解) 模板: #include<iostream> #include<cstdio> #include<cstring> #include ...

  3. Netty入门(八)构建Netty HTTP/HTTPS应用

    HTTP/HTTPS 是最常见的一种协议,这节主要是看一下 Netty 提供的 ChannelHaandler. 一.HTTP Decoder,Encoder 和 Codec HTTP 是请求-响应模 ...

  4. 2.HBase In Action 第一章-HBase简介(1.1数据管理系统:快速学习)

    Relational database systems have been around for a few decades and have been hugely successful in so ...

  5. PHP实现微信发红包功能2

    <?php class wxPay { //配置参数信息 const SHANGHUHAO = "1430998xxx";//商户号 const PARTNERKEY = & ...

  6. openfalcon agent 监控数据

    [root@nginx1 ~]# cat /proc/sys/fs/file-nr 791435 已分配文件句柄的数目  已使用文件句柄的数目  文件句柄的最大数目  [root@nginx1 ~]# ...

  7. linux ssh 应用

    linux 服务器 连接另一个linux服务器 ssh 用户名@IP地址 linux 服务器传输文件到另一个linux服务器 scp 文件名(可多个)  用户名@IP地址:传到的目录 /home

  8. 爬虫header和cookie

    def on_start(self): self.crawl('http://bbs.byr.cn/board/Python', headers={'X-Requested-With': 'XMLHt ...

  9. 基于DirectX的半球形天空类的C++和C#实现

    目前,天空绘制主要有三种方法:矩形天空.天空盒和球形天空. (1)矩形天空使用一个与地面垂直或呈一定夹角的矩形表示天空,用接近于天空的颜色或云彩纹理贴于矩形上.这种方法简单易行,但需要不断调整视角或观 ...

  10. dom阻止事件冒泡

    通常有两种事件流模型,一种是冒泡,一种是捕获.顾名思义,冒泡就是从内往外传播,捕获就是从外往里传播. 对于dom事件,就是这样的.比如,有两个父子div. <div id="pdiv& ...