Question

463. Island Perimeter

Solution

题目大意:给出一个二维数组1表示陆地0表示海,求陆地的周长

思路:

重新构造一张地图grid2即一个二维数组,比原数组大一圈,即长宽都大2
一个点在原地图坐标是(i,j),那么在重新构造的坐标就是(i+1,j+1)
遍历原地图,如果一是陆地,就遍历这个点的周围是否是海,如果是海线周长就加1

Java实现:

public int islandPerimeter(int[][] grid) {
int total = 0;
int[][] grid2 = new int[grid.length+2][grid[0].length+2];
for (int i=0; i<grid.length; i++) {
for (int j=0; j<grid[0].length; j++) {
if (grid[i][j] == 1) {
grid2[i+1][j+1] = 1;
}
}
} for (int i=0; i<grid.length; i++) {
for (int j=0; j<grid[i].length; j++) {
int x = i+1;
int y = j+1;
if (grid2[x][y] == 0) continue;
// left
total += grid2[x-1][y]==1?0:1;
// right
total += grid2[x+1][y]==1?0:1;
// top
total += grid2[x][y-1]==1?0:1;
// bottom
total += grid2[x][y+1]==1?0:1;
}
}
return total;
}

463. Island Perimeter - LeetCode的更多相关文章

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

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

  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 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 减去相交部分 参考资料 日期 题目地址:https: ...

  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. LeetCode 463 Island Perimeter 解题报告

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

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

  7. [LeetCode&Python] Problem 463. 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 - O(MN)- (C++) - 解题报告

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

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

随机推荐

  1. Linux下切换python2和python3

    为什么需要有两个版本的Python Python2和Python3不兼容是每个接触过Python的开发者都知道的事,虽说Python3是未来,但是仍然有很多项目采用Python2开发.Linux的许多 ...

  2. 什么是CSS Modules ?我们为什么需要他们

    原文地址:https://css-tricks.com/css-mo...最近我对CSS Modules比较好奇.如果你曾经听说过他们,那么这篇博客正适合你.我们将去探索它的目的和主旨.如果你同样很好 ...

  3. 可想实现一个自己的简单jQuery库?(九)

    Lesson-8 事件机制 在讲事件机制之前呢,我们有一个很重要的东西要先讲,那就是如何实现事件委托(代理). 只有必须先明白了如何实现一个事件委托,我们才能更好的去实现on和off.在我看来,on和 ...

  4. Python使用递归绘制谢尔宾斯基三角形

    谢尔宾斯基三角形使用了三路递归算法,从一个大三角形开始,通过连接每一个边的中点,将大三角型分为四个三角形,然后忽略中间的三角形,依次对其余三个三角形执行上述操作. 运行效果: 源代码: 1 impor ...

  5. vue引入echarts

    效果图: 1.安装Echarts :     npm install echarts -S 或者使用国内的淘宝镜像: 安装: npm install -g cnpm --registry=https: ...

  6. pip 和 Conda 镜像站配置

    如果你经常使用 Python,那么你对 pip 和 Conda 一定不陌生,它们作为包管理器,可以非常方便的帮助我们下载需要的 Python 包,但是受限于大多 Python 包的服务器在国外,国内下 ...

  7. kafka快速入门到精通

    目录 1. 消息队列两种模式 1.1 消息队列作用 1.2 点对点模式(一对一,消费者主动拉取数据,消息收到后消息删除) 1.3 发布/订阅模式(一对多,消费数据之后不会删除消息) 1.4 kafka ...

  8. iOS全埋点解决方案-控件点击事件

    前言 ​ 我们主要介绍如何实现控件点击事件($AppClick)的全埋点.在介绍如何实现之前,我们需要先了解一下,在 UIKit 框架下,处理点击或拖动事件的 Target-Action 设计模式. ...

  9. python---二分查找的实现

    from cal_time import get_running_time @get_running_time def bin_search(li, val): """ ...

  10. SQLite 数据库使用记录

    SQLite 数据库使用记录 官网 https://www.sqlite.org/index.html 下载地址 https://www.sqlite.org/download.html 参考资料 S ...