1、题目

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:

2、代码实现

public class Solution {public int islandPerimeter(int[][] grid) {if (grid == null)return 0;//gril.length就是行数 row,grid.length就是列数grilint row = grid.length;int col = grid[0].length;int sum = 0, count = 0;for (int i = 0; i < row; ++i) {for (int j = 0; j < col; ++j) {if (grid[i][j] == 1) {if (i - 1 == -1) {count++;} else {if (grid[i - 1][j] == 0) {count++;}}if (i + 1 == row) {count++;} else {if (grid[i + 1][j] == 0) {count++;}}if (j - 1 == -1) {count++;} else {if (grid[i][j - 1] == 0) {count++;}}if (j + 1 == col) {count++;} else {if (grid[i][j + 1] == 0) {count++;}}}}}return count;    }
}

LeetCode之Island Perimeter相关推荐

  1. LeetCode 463. Island Perimeter

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

  2. 歪门邪道 Leetcode 463 Island Perimeter

    题目是求一个由多个边长为1的正方形连接而成的多边形的周长,一般来说,遍历整个数组,对值为1的陆地项做判断,周围有多少个水区域就加上多少边长,即对a[I][j]来说,判断a[i-1][j], a[i+1 ...

  3. Leetcode PHP题解--D38 463. Island Perimeter

    2019独角兽企业重金招聘Python工程师标准>>> 463. Island Perimeter 题目链接 463. Island Perimeter 题目分析 给定一个二维数组, ...

  4. 岛屿数量JAVA_LeetCode刷题 463. Island Perimeter 岛屿的周长 Java

    LeetCode刷题 463. Island Perimeter 岛屿的周长 Java LeetCode刷题 463. Island Perimeter 岛屿的周长 Java 题目介绍 链接:http ...

  5. D38 463. Island Perimeter

    463. Island Perimeter 题目链接 463. Island Perimeter 题目分析 给定一个二维数组,代表一个二维表格. 里面包含0和1两种数字. 1代表有内容,0代表没有. ...

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

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

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

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  8. island of java_LeetCode算法题-Island Perimeter(Java实现)

    这是悦乐书的第238次更新,第251篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第105题(顺位题号是463).您将获得一个二维整数网格形式的地图,其中1代表土地,0代 ...

  9. 463. Island Perimeter 岛屿的周长

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

最新文章

  1. ELFhash - 优秀的字符串哈希算法
  2. 机器学习中的DCGAN-Tensorflow:用于更稳定的训练
  3. Unity3d5.0 新UI之2048
  4. 通过vsphere client创建虚拟机并且安装windows 2008 64位操作系统及网络配置
  5. axios get post下载文件
  6. 剑指offer——9.用两个栈实现队列
  7. Q143:FS,物质导数(Material Derivative)
  8. 用c语言编写五子棋用vs写,c语言写的五子棋人与人对战
  9. XCL-Charts图表库中柱形图的同源风格切换介绍
  10. 3.26 初接触Python
  11. 数据挖掘十大经典算法
  12. 洛谷P3354 [IOI2005]Riv 河流 题解
  13. 智能暖风机——FAQ
  14. html文字超过部分显示为省略号
  15. 一文分析 快应用技术架构及业务前景
  16. ArcBlock世界第一区块链基石生态系统
  17. 手持振弦传感器VH03读数仪测读仪频率仪地质灾害土木工程自动化监测
  18. 常见RISC-V介绍
  19. 第二次作业熟悉使用工具
  20. 电磁场与仿真软件(13)

热门文章

  1. 数据资产纳入国资保值增值考核
  2. 部署Dotnet Core应用到Kubernetes(二)
  3. 项目开发中经常有一些被嫌弃的小数据,现在全丢给 FastDFS
  4. WPF中的Data Binding调试指南
  5. .NET开发者省份分布排名
  6. 【新插件发布】AzureAD运维Excel版插件,增删改查快10倍c以上!
  7. 我们为什么推荐在Json中使用string表示Number属性值
  8. BeetleX.Redis基础操作和对象格式
  9. .Net Core 认证组件源码解析
  10. 动手造轮子:实现一个简单的依赖注入(二) --- 服务注册优化