详见:https://leetcode.com/problems/island-perimeter/description/

C++:

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

参考:https://www.cnblogs.com/grandyang/p/6096138.html

463 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. Leetcode463.Island Perimeter岛屿的周长

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

  3. 463. Island Perimeter岛屿周长

    [抄题]: You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 re ...

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

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

  5. 463. Island Perimeter - LeetCode

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

  6. LeetCode 463 Island Perimeter 解题报告

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

  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. LeetCode 463. Island Perimeter (岛的周长)

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

  9. 463. Island Perimeter

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

随机推荐

  1. STL 源代码剖析 算法 stl_algo.h -- nth_element

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie nth_element ---------------------------------- ...

  2. 10 逻辑完善以及bug修复

    进行到这里,我们应用开发已经接近尾声,我这里基本就是应用开发的记录过程,讲解的东西很少,有问题可以在评论区讨论呦.下面进入最后调整的阶段. 预览我们的应用,会发现首页的职位列表,也会显示收藏的星星图标 ...

  3. 安装BIRT Chart Engine的时候,提示Cannot complete the install because one or more required items could not be

    http://wiki.eclipse.org/BIRT_Update_Site_URL 每个eclipse对应的BIRT版本 help-install new software: http://do ...

  4. Django初识二

    1,在django中用于提交的form表单中的三要素: 1.1>form标签要有action和method,上传文件需要额外指定的enctype 1.2>获取用户输入的标签要有name属性 ...

  5. Hibernate 之 Why?

    本文主要是从一个宏观的角度来认识Hibernate,对为什么用Hibernate进行一些说明,通过指导并了解Hibernate的特性及其优缺点可以让我们在以后的项目中根据具体的情况进行选择. Hibe ...

  6. diy数据库(二)--网络通信类

    一.首先,我们先实现OSS层的ossSocket类.供数据库client和数据库引擎进行通信 友情提示:相应上面的类图的头文件和源码附在了本文的最以下. int _fd ;//socket的文件描写叙 ...

  7. YTU 2900: F-A Simple Question

    2900: F-A Simple Question 时间限制: 1 Sec  内存限制: 128 MB 提交: 66  解决: 24 题目描述 今天,pasher打算在一个浪漫的花园和他的搭档们聚餐, ...

  8. 路由器的LAN、WAN、WLAN的区别

    路由器的LAN.WAN.WLAN的区别 好多朋友在群内问我路由器如何配置,本来还耐心解答,但是他竟然连LAN.WAN都分不清,瞬间就没了帮助的热情.借这篇经验,小编简单讲一下路由常见的LAN.WAN. ...

  9. javascript 省、市、地县三级联动

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD&g ...

  10. Python装饰器单例

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/dutsoft/article/details/52057981#!/usr/bin/python#c ...