Leetcode463.Island Perimeter岛屿的周长
给定一个包含 0 和 1 的二维网格地图,其中 1 表示陆地 0 表示水域。
网格中的格子水平和垂直方向相连(对角线方向不相连)。整个网格被水完全包围,但其中恰好有一个岛屿(或者说,一个或多个表示陆地的格子相连组成的岛屿)。
岛屿中没有“湖”(“湖” 指水域在岛屿内部且不和岛屿周围的水相连)。格子是边长为 1 的正方形。网格为长方形,且宽度和高度均不超过 100 。计算这个岛屿的周长。
示例 :
输入: [[0,1,0,0], [1,1,1,0], [0,1,0,0], [1,1,0,0]] 输出: 16
class Solution {
public:
int islandPerimeter(vector<vector<int>>& grid) {
int r = grid.size();
if(r == 0)
return 0;
int c = grid[0].size();
int cnt = 0;
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
if(grid[i][j] == 0)
continue;
//上下左右
if(i - 1 < 0 || grid[i - 1][j] == 0)
cnt++;
if(i + 1 >= r || grid[i + 1][j] == 0)
cnt++;
if(j - 1 < 0 || grid[i][j - 1] == 0)
cnt++;
if(j + 1 >= c || grid[i][j + 1] == 0)
cnt++;
}
}
return cnt;
}
};
Leetcode463.Island Perimeter岛屿的周长的更多相关文章
- LeetCode 463. Island Perimeter岛屿的周长 (C++)
题目: You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 repr ...
- 463 Island Perimeter 岛屿的周长
详见:https://leetcode.com/problems/island-perimeter/description/ C++: class Solution { public: int isl ...
- Leetcode-463 Island Perimeter
#463. Island Perimeter You are given a map in form of a two-dimensional integer grid where 1 represe ...
- Leetcode463. Island Perimeter
题目 给定一个包含 0 和 1 的二维网格地图,其中 1 表示陆地 0 表示水域. 网格中的格子水平和垂直方向相连(对角线方向不相连).整个网格被水完全包围,但其中恰好有一个岛屿(或者说,一个或多个表 ...
- [LeetCode] 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 岛的周长
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- [Swift]LeetCode463. 岛屿的周长 | Island Perimeter
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- C#LeetCode刷题之#463-岛屿的周长(Island Perimeter)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3794 访问. 给定一个包含 0 和 1 的二维网格地图,其中 1 ...
随机推荐
- eclipse 克隆 https 地址的 Git 仓库报错:cannot open git-upload-pack
解决方法:Window >Preferences >Team>Git>User settings点击Add Entry设置key:http.sslVerify value:fa ...
- linux /bin/find 报错:paths must precede expression 及find应用
1.问题描述,运行下面的命令,清楚日志 [resin@xx ~]$ ssh xxx "/usr/bin/find /data/logs/`dirname st_qu/stdout.log` ...
- 【默默努力】h5-game-heroVSmonster
先放下作者大大的项目地址:https://github.com/yangyunhe369/h5-game-heroVSmonster 然后游戏的效果为 截动图的按键与游戏按键应该冲突,我就截几张图片了 ...
- html--垂直居中
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- c++ static关键字的作用
1.被申明的函数或值无法被其他源文件使用 2.static的第二个作用是保持变量内容的持久.(static变量中的记忆功能和全局生存期) 存储在静态数据区的变量会在程序刚开始运行时就完成初始化,也是唯 ...
- css---switch开关
html: <label><input class="mui-switch" type="checkbox"> 默认未选中</la ...
- 数据交互 axios 的使用
Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中. ##Axios npm version build status code coverage npm ...
- SpringBoot 03_利用FastJson返回Json数据
自上一节:SpringBoot 02_返回json数据,可以返回json数据之后,由于有些人习惯于不同的Json框架,比如fastjson,这里介绍一下如何在SpringBoot中集成fastjson ...
- 15_K-近邻算法之入住位置预测
案例:本次大赛的目的是预测一个人想签入到哪个地方.对于本次比赛的目的,Facebook的创建一 个人造的世界,包括位于10公里的10平方公里超过10万米的地方.对于一个给定的坐标,你的任务是返回最有可 ...
- JZOJ5894【NOIP2018模拟10.5】同余方程
题目 Description