题目

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

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

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

示例 :

输入:
[[0,1,0,0],
[1,1,1,0],
[0,1,0,0],
[1,1,0,0]] 输出: 16 解释: 它的周长是下面图片中的 16 个黄色的边:

考点


思路

solution1

如果这个格子有岛屿,先假设算4个边,判断左边和上边是否有岛屿,如果左边有岛屿,res减去2,上边同理。

solution2

如果这个格子有岛屿,只有在该格子是在岛屿边界处,就计算一条边,岛屿边界处:在数组最左边,或者左边格子没有岛屿,同理,判断上下左右四个方向的格子。


代码

solution1

class Solution {
public:
int islandPerimeter(vector<vector<int>>& grid) {
if(grid.empty()||grid[0].empty())
{
return 0;
}
int rows=grid.size();
int cols=grid[0].size();
int len=0;
for(int row=0;row<rows;row++)
{
for(int col=0;col<cols;col++)
{
if(grid[row][col]==0) continue;
len+=4;
if(row>0&&grid[row-1][col]==1) len-=2;//上面
if(col>0&&grid[row][col-1]==1) len-=2;//左边 }
}
return len;
}
};

solution 2

class Solution {
public:
int islandPerimeter(vector<vector<int>>& grid) {
if(grid.empty()||grid[0].empty())
{
return 0;
}
int rows=grid.size();
int cols=grid[0].size();
int len=0;
for(int row=0;row<rows;row++)
{
for(int col=0;col<cols;col++)
{
if(grid[row][col]==0) continue; if(row==0||grid[row-1][col]==0) len++;//上
if(col==0||grid[row][col-1]==0) len++;//左
if(row==rows-1||grid[row+1][col]==0) len++;//下,这里注意数组越界
if(col==cols-1||grid[row][col+1]==0) len++;//右
}
}
return len;
}
};

问题

Leetcode463. Island Perimeter的更多相关文章

  1. Leetcode-463 Island Perimeter

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

  2. Leetcode463.Island Perimeter岛屿的周长

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

  3. LeetCode_463. Island Perimeter

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

  4. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  5. 463. Island Perimeter - LeetCode

    Question 463. Island Perimeter Solution 题目大意:给出一个二维数组1表示陆地0表示海,求陆地的周长 思路: 重新构造一张地图grid2即一个二维数组,比原数组大 ...

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

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

  7. [LeetCode] Island Perimeter 岛屿周长

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

  8. 463. Island Perimeter

    https://leetcode.com/problems/island-perimeter/ 在一个N×N的矩阵中,N<100,1代表岛,0代表海,岛内没有海,求岛的周长 [[0,1,0,0] ...

  9. LeetCode Island Perimeter

    原题链接在这里:https://leetcode.com/problems/island-perimeter/ 题目: You are given a map in form of a two-dim ...

随机推荐

  1. phpmyadmin 开放远程登录的权限

    *linux下的修改* 在phpmyadmin.conf 加上如下试一下 <Directory "phpmyadmin路径">     AllowOverride No ...

  2. activeMq 配置(一)

    基础知识补充 1.ActiveMQ从入门到精通(一)https://www.jianshu.com/p/ecdc6eab554c 2.ActiveMQ从入门到精通(二)https://www.jian ...

  3. 【ubuntu】安装之后要安装的一些东西

    问题1: ubuntu默认没有安装vim,出现: root@evelyn-virtual-machine:~# vim /etc/ssh/sshd_config The program 'vim' c ...

  4. pop协议,邮件密码嗅探 使用说明

    思路与源代码 可以查看 https://www.cnblogs.com/likehc/p/10140174.html 因为软件用的是Wincap,所以 第一步安装下 WinPcap 第二步,打开 sn ...

  5. hibernate课程 初探一对多映射2-2 Myeclipse进行hibernate基本配置

    本节主要内容: 1 myeclipse 中hibernate jar包,mysql连接jar包 导入 2 hibernate.cfg.xml的配置 1 一对一映射已经配置过 2 hibernate.c ...

  6. 在UITableView中识别左右滑动,实现上下翻页的功能

    目前有三种方案: 1. UIScrollView + UITableView. 实现方法,在UIScrollView中,加入UITableView即可 设置UIScrollView的代理和方法 - ( ...

  7. C#开发短信的方法和简介(转)

    http://ce.sysu.edu.cn/hope2008/Education/ShowArticle.asp?ArticleID=6337(来自) 自己收藏哈子

  8. textarea存起来的数据把空格也存起来

    textarea的属性wrap="hard"可以把换行的内容也存起来. <html> <head> <title>这是一个小测试</tit ...

  9. 内表转WORD

    组合HTML字符串的方法来导出WORD文件 DATA: BEGIN OF wa_html, zhtml(), END OF wa_html, gt_html LIKE TABLE OF wa_html ...

  10. 关于 supersocket 不能通过Bootstrap 启动

    App.config内容   <configSections> <section name="superSocket" type="SuperSocke ...