给定一个包含 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岛屿的周长的更多相关文章

  1. 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 ...

  2. 463 Island Perimeter 岛屿的周长

    详见:https://leetcode.com/problems/island-perimeter/description/ C++: class Solution { public: int isl ...

  3. Leetcode-463 Island Perimeter

    #463. Island Perimeter You are given a map in form of a two-dimensional integer grid where 1 represe ...

  4. Leetcode463. Island Perimeter

    题目 给定一个包含 0 和 1 的二维网格地图,其中 1 表示陆地 0 表示水域. 网格中的格子水平和垂直方向相连(对角线方向不相连).整个网格被水完全包围,但其中恰好有一个岛屿(或者说,一个或多个表 ...

  5. [LeetCode] 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 岛的周长

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...

  8. [Swift]LeetCode463. 岛屿的周长 | Island Perimeter

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...

  9. C#LeetCode刷题之#463-岛屿的周长​​​​​​​(Island Perimeter)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3794 访问. 给定一个包含 0 和 1 的二维网格地图,其中 1 ...

随机推荐

  1. 使用CEfSharp之旅(5)CEFSharp 隔离Cookie

    原文:使用CEfSharp之旅(5)CEFSharp 隔离Cookie 版权声明:本文为博主原创文章,未经博主允许不得转载.可点击关注博主 ,不明白的进群191065815 我的群里问 https:/ ...

  2. soj98 卡牌

    题意:一共有n张牌,每张牌有三个属性ai,bi,ci.问在属性上限为A,B,C的所有牌中有多少张牌满足至少有两个属性可以完全压制(严格大于)那n张牌? n<=50W. 标程: #include& ...

  3. 0829NOIP模拟测试赛后总结

    这次发誓不会咕咕咕! 80分rank30完美爆炸. 拿到题目苏轼三连???貌似三篇古诗文我都会背啊hhh.爆零警告 T1没啥思路,打完暴力后想了大约20分钟决定分解个因数,在b次方中每一次方选择一个约 ...

  4. 聊聊MVC和模块化以及MVVM和组件化

    原文链接 小寒的博客,带你理解更深的世界 面向对象,模块化和MVC 面向对象是指把写程序映射到现实生活,从而一来逻辑性更强,更容易写好代码,二来代码很贴切,通俗易懂,更被人理解,三来更加容易拓展和管理 ...

  5. Android基础控件TextClock和Chronometer的使用

    1.简介 DigitalClock, TextClock,AnalogClock,Chronometer其中DigitalClock和AnalogClock废弃了! TextClock是在Androi ...

  6. Chrome浏览器console控制台不打印任何js错误信息

    手欠在Chrome控制台在错误信息,右键:Hide messages from vue 看不到 报错信息 这里删除成 默认的Filter 报错就出现了

  7. CF 1281B Azamon Web Services

    原题链接:http://codeforces.com/problemset/problem/1281/B 题目大意: 给你两个字符串 s 和 c ,最多经过一次变换,使s的字典序小于c,输出变换后的s ...

  8. Cocos2d-x 创建工程python脚本

    @echo offset /p projectName=请输入项目名称:if "%projectName%"=="" goto inputErrorset /p ...

  9. SDOI2019 R2退役记

    还是退役了呀 Day -1 早上loli发了套题结果啥都不会 之后胡爷爷就秒了道数据结构 不过也没什么人做,于是全机房都在愉快的划水 下午来机房打了场luogu的\(rated\)赛,还是啥都不会 之 ...

  10. vue生成条形码/二维码/带logo二维码

    条形码:https://blog.csdn.net/dakache11/article/details/83749410 //安装 cnpm install @xkeshi/vue-barcode / ...