原题

原题链接

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:

[[0,1,0,0],

[1,1,1,0],

[0,1,0,0],

[1,1,0,0]]

Answer: 16

Explanation: The perimeter is the 16 yellow stripes in the image below:

给定一个二维整数的方格地图,1代表陆地,0代表有水的地方。每个方格都是垂直或者水平连接的,没有斜线。被水包围的中间有一个小岛(小岛可能包含了很多个的陆地方格),小岛里没有岛中湖(内部的水和外部的水是连通的)。每个方格单元的边长都是1。各个地方都是成直角的,长度宽度不超过100,判断小岛的边长。

思路

判断其四面是否有陆地,四面都有陆地的对周长无贡献,两面有陆地的贡献周长为2,一面有陆地的贡献周长为3,四面都没有陆地的贡献周长为4。

代码

class Solution {
public:
int islandPerimeter(vector<vector<int>>& grid) {
int sum=0;
int m=grid.size(),n=grid[0].size();
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
if(grid[i][j]){
if(i==0||grid[i-1][j]==0) sum++;
if(i==m-1||grid[i+1][j]==0) sum++;
if(j==0||grid[i][j-1]==0) sum++;
if(j==n-1||grid[i][j+1]==0) sum++;
}
return sum;
}
};

LeetCode - 463. Island Perimeter - O(MN)- (C++) - 解题报告的更多相关文章

  1. LeetCode 463 Island Perimeter 解题报告

    题目要求 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 rep ...

  2. LeetCode 463. Island Perimeter (岛的周长)

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

  3. [LeetCode] 463. Island Perimeter 岛的周长

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

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

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

  6. 3. leetcode 463 Island Perimeter

    思路:设原始周长为4*节点数,每当出现一次相邻的情况,原始周长会减2.

  7. 【LeetCode】Island Perimeter 解题报告

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

  8. 463. Island Perimeter - LeetCode

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

  9. 【LeetCode】697. Degree of an Array 解题报告

    [LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...

随机推荐

  1. Tomcat问题之 启动 Cannot find /usr/local/tomcat/bin/setclasspath.sh

    在linux启动startup命令报Cannot find /usr/local/tomcat/bin/setclasspath.sh  使用: unset CATALINA_HOME命令得以解决   ...

  2. Css Sprite(雪碧图、精灵图)<图像拼合技术>

    一.精灵图使用场景: 二.Css Sprite(优点) 减少图片的字节. 减少网页的http请求,从而大大的提高页面的性能. 解决了网页设计师在图片命名上的困扰,只需对一张集合的图片上命名就可以了,不 ...

  3. laravel4.2 Redis 使用

    laravel4.2 Redis 使用 配置文件,app/config/database.php 'redis' => array( 'cluster' => false, 'defaul ...

  4. Java学习笔记二十:Java中的内部类

    Java中的内部类 一:什么是内部类: (1).什么是内部类呢? 内部类( Inner Class )就是定义在另外一个类里面的类.与之对应,包含内部类的类被称为外部类. (2).那为什么要将一个类定 ...

  5. linux静态链接库

    库 库是写好的现有的,成熟的,可以复用的代码.现实中每个程序都要依赖很多基础的底层库,不可能每个人的代码都从零开始,因此库的存在意义非同寻常 本质上来说库是一种可执行代码的二进制形式,可以被操作系统载 ...

  6. Go Web Gin EasyUI 框架

    项目结构 第三方: 1.gin微服务框架 2.EasyUI框架

  7. HyperLedger Fabric 1.4 问题汇总(16)

    16.1 在运行e2e_cli例子时,执行./network_setup.sh up,出现错误:网络搭建之network e2ecli_default not found 问题原因: End-2-En ...

  8. vs2013工程配置

    1. 目标文件生成路径配置: 直接改在工程同级目录下 x64\debug目录下: 2. 调试工程路径配置: 命令-----参照物为工程 工作目录----参照物为运行程序 3.  拷贝工程: bat的写 ...

  9. 20145209刘一阳 《网络对抗》逆向及BOF基础实践

    直接修改程序机器指令,改变程序执行流程 在正式开始实践之前,先对pwn1文件做个备份,以便修改后可以及时恢复到初始状态: 使用指令objdump -d 20145209 | more对目标文件进行反汇 ...

  10. Splay初学习

    例题传送门 听YZ哥哥说Splay是一种很神奇的数据结构,所以学习了一下它的最基本操作.O(1)的Spaly. 伸展树(Splay Tree),也叫分裂树,是一种二叉排序树,它能在O(logn)内完成 ...