题目:

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. java使用elasticsearch进行模糊查询之must使用-项目中实际使用

    java使用elasticsearch进行多个条件模糊查询 文章说明: 1.本篇文章,本人会从java连接elasticsearch到查询结果生成并映射到具体实体类(涵盖分页功能) 2.代码背景:el ...

  2. 【Alpha 冲刺】8/12

    今日任务总结 人员 今日原定任务 完成情况 遇到问题 贡献值 胡武成 完善API文档,并初步使用SpringMVC产生编写部分API 未完成 白天有事外出,晚上因为jdk版本过高,配置SpringMV ...

  3. BZOJ3578:GTY的人类基因组计划2(集合hash,STL)

    Description GTY召唤了n个人来做实验,GTY家的房子很大,有m个房间一开始所有人都在1号房间里,GTY会命令某人去某个房间等待做实验,或者命令一段区间的房间开始实验,实验会获得一些实验信 ...

  4. [JSOI2009]球队收益

    题目 这题好神啊 我们发现一个球队的总比赛场数是确定的,设第\(i\)支球队一共进行了\(s_i\)场比赛 于是这个球队的收益就是\(c_i\times x^2+d_i(s_i-x)^2\) 我们拆开 ...

  5. VC++编译错误error C2065: “HANDLE”: 未声明的标识符及添加winbase.h后提示winbase.h(243): error C2146: 语法错误: 缺少“;”(在标识符“Internal”的前面)的解决办法

    问题描述: VC++程序编译时提示错误:error C2065: “HANDLE”: 未声明的标识符等众多错误提示,如下所示: error C2065: “HANDLE”: 未声明的标识符 error ...

  6. leetcode18—4Sum

    Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...

  7. PAT A1004 Counting Leaves (30 分)——树,DFS,BFS

    A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...

  8. DQN(Deep Reiforcement Learning) 发展历程(三)

    目录 不基于模型(Model-free)的预测 蒙特卡罗方法 时序差分方法 多步的时序差分方法 参考 DQN发展历程(一) DQN发展历程(二) DQN发展历程(三) DQN发展历程(四) DQN发展 ...

  9. bash 定时任务

    time1=$(date +%s -d '2014-01-13 22:46:05') for((;;)) do time2=$(date +%s) time3=$((time1 - time2)) ] ...

  10. linux中原子操作实现方式

    原子操作提供了指令原子执行,中间没有中断.就像原子被认为是不可分割颗粒一样,原子操作(atomic operation)是不可分割的操作. 如下面简单的例子: Thread 1             ...