给定一个包含 0 和 1 的二维网格地图,其中 1 表示陆地 0 表示水域。

网格中的格子水平和垂直方向相连(对角线方向不相连)。整个网格被水完全包围,但其中恰好有一个岛屿(或者说,一个或多个表示陆地的格子相连组成的岛屿)。

岛屿中没有“湖”(“湖” 指水域在岛屿内部且不和岛屿周围的水相连)。格子是边长为 1 的正方形。网格为长方形,且宽度和高度均不超过 100 。计算这个岛屿的周长。

示例 :

输入: [[0,1,0,0], [1,1,1,0], [0,1,0,0], [1,1,0,0]] 输出: 16

  1. class Solution {
  2. public:
  3. int islandPerimeter(vector<vector<int>>& grid) {
  4. int r = grid.size();
  5. if(r == 0)
  6. return 0;
  7. int c = grid[0].size();
  8. int cnt = 0;
  9. for(int i = 0; i < r; i++)
  10. {
  11. for(int j = 0; j < c; j++)
  12. {
  13. if(grid[i][j] == 0)
  14. continue;
  15. //上下左右
  16. if(i - 1 < 0 || grid[i - 1][j] == 0)
  17. cnt++;
  18. if(i + 1 >= r || grid[i + 1][j] == 0)
  19. cnt++;
  20. if(j - 1 < 0 || grid[i][j - 1] == 0)
  21. cnt++;
  22. if(j + 1 >= c || grid[i][j + 1] == 0)
  23. cnt++;
  24. }
  25. }
  26. return cnt;
  27. }
  28. };

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. P1305 新二叉树 /// 二叉树的先序遍历

    题目大意: https://www.luogu.org/problemnew/show/P1305 由题目可知,输入首位为 子树的根 其后为其左右儿子 则除各行首位后的位置中 没有出现的那个字母肯定为 ...

  2. HDU - 2222,HDU - 2896,HDU - 3065,ZOJ - 3430 AC自动机求文本串和模式串信息(模板题)

    最近正在学AC自动机,按照惯例需要刷一套kuangbin的AC自动机专题巩固 在网上看过很多模板,感觉kuangbin大神的模板最为简洁,于是就选择了用kuangbin大神的模板. AC自动机其实就是 ...

  3. 历经小半宿吧。哎,终于搭建好了Linux-C的环境

    小小地庆祝一下吧   继续努力学习,把Linux-C熟悉一下,争取做到会用吧...   我想应该不难   我熟悉的 iostream 还存在   可惜,微软基础类库不在了...

  4. Android开发 输入法调用学习

    方法一(如果输入法在窗口上已经显示,则隐藏,反之则显示) InputMethodManager imm = (InputMethodManager) getSystemService(Context. ...

  5. 解决Pycharm无法导入包问题 Unresolved reference

    在pycharm中设置source路径 file–>setting–>project:server–>project structure 将放package的文件夹设置为source ...

  6. CF891D Sloth

    题意:给你一棵树,你选择删掉一条边,再加上一条边(也要保证为树),问最后树上的节点能够两两完美匹配的加删边方案数? n<=5e5. 标程: #include<cstdio> #inc ...

  7. Xstream 解析xml文件内容

    刚刚接手的一个项目,接到一个对接用户数据的需求,对方使用的是xml格式来传输文件,特此记下解析该类文件的方法 public interface XmlResolver<T> { XStre ...

  8. nodejs vue 微信公众号开发(二)申请微信测试号

    1.打开微信测试公众号开发平台http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login 扫码登陆

  9. Django + Uwsgi + Nginx 的生产环境部署实战

    目录 Django + Uwsgi + Nginx 的生产环境部署实战 安装Uwsgi 一.使用命令来启动django项目 二.使用配置文件来启动我们的Django项目 安装Nginx 配置Nginx ...

  10. 关于H5裁剪图片后,直传阿里云的一些问题

    这段时间在工作中碰到一个需要在h5裁剪图像,然后直传阿里云的需求.图中遇到了一些小问题,分享出来大家都看看. h5裁剪图像:cropper.js是一个神器啊关于用法,网上可以收罗出大量的帖子,这里我就 ...