LeetCode 463. Island Perimeter岛屿的周长 (C++)
题目:
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++)的更多相关文章
- [LeetCode] 463. Island Perimeter 岛的周长
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- 463 Island Perimeter 岛屿的周长
详见:https://leetcode.com/problems/island-perimeter/description/ C++: class Solution { public: int isl ...
- LeetCode 463 Island Perimeter 解题报告
题目要求 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 rep ...
- Leetcode463.Island Perimeter岛屿的周长
给定一个包含 0 和 1 的二维网格地图,其中 1 表示陆地 0 表示水域. 网格中的格子水平和垂直方向相连(对角线方向不相连).整个网格被水完全包围,但其中恰好有一个岛屿(或者说,一个或多个表示陆地 ...
- LeetCode 463. Island Perimeter (岛的周长)
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- 463. Island Perimeter岛屿周长
[抄题]: You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 re ...
- 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 ...
- 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 ...
- 3. leetcode 463 Island Perimeter
思路:设原始周长为4*节点数,每当出现一次相邻的情况,原始周长会减2.
随机推荐
- Java源码分析(1):二分查找 + 循环递归实现
源代码 源码地址 public static int binarySearch(int[] a, int key) { return binarySearch0(a, 0, a.length, key ...
- 阿里八八Alpha阶段Scrum(3/12)
今日进度 叶文滔: 实现了悬浮按钮的拖动. 问题困难:第三方库调入不成功,多级悬浮按钮的实现仍未完成. 刘晓: 完成注册.修改密码的UI部分,创建了注册Activity,修改密码Activity. 问 ...
- 常用npm 命令
npm 官方网站:npm的使用说明 安装模块 npm install 安装当前目录package.json文件中配置的dependencies模块 安装本地的模块文件 npm install ...
- Flume学习之路 (二)Flume的Source类型
一.概述 官方文档介绍:http://flume.apache.org/FlumeUserGuide.html#flume-sources 二.Flume Sources 描述 2.1 Avro So ...
- 哪些地方会出现css阻塞,哪些地方会出现js阻塞?
Js的阻塞特性: 所有浏览器在下载JS的时候,会阻止一切其他活动,比如其他资源的下载,内容的呈现等等.直到JS下载.解析.执行完毕后才开始继续并行下载其他资源并呈现内容.为了提高用户体验,新一代浏览器 ...
- Springmvc常见问题
问题一:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userCont ...
- css3动画功能介绍
一:过渡动画---Transitions 含义:在css3中,Transitions功能通过将元素的某个属性从一个属性值在指定的时间内平滑过渡到另一个属性值来实现动画功能. Transitions属性 ...
- Leetcode——64. 最小路径和
题目描述:题目链接 同样对于这个问题,我们可以考虑用动态规划来解决. 解决动态规划常见的三个步骤: 1:问题的归纳.对于 i,j 位置上的最短路径可以用d[ i ][ j ]表示. 2:归纳递推式:d ...
- docker push images login -u harbor 问题记录 https 证书
1.[root@dev-100 Desktop]# docker login -u clouder -p engine harbor.xiaowei.com 2.docker tag busybox: ...
- MVC 拦截指定的action
有时,我们需要在特定的一些aciton中做校验.比如:验证是否登录.实现方式有两种: 一.编写一个公共的方法专门用于实现是否登录的验证,然后在每个需要进行验证的aciton的头部去调用该方法,根据方法 ...