【LeetCode】463. Island Perimeter 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/island-perimeter/description/
题目描述
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:
Input:
[[0,1,0,0],
[1,1,1,0],
[0,1,0,0],
[1,1,0,0]]
Output: 16
Explanation: The perimeter is the 16 yellow stripes in the image below:
题目大意
给了一个二维数组表示的一张地图,如果某个位置是1,那么表示的是陆地,位置是0表示的是海洋。已知陆地连在一起的话会构成一个小岛,而且这个地图里面只有一个小岛。求小岛的周长。
解题方法
减去相交部分
直接求解好像很难,可以使用一个简单的方法:每个位置的周长是4,如果和另一个陆地相连,那么这两个陆地周长和减去2.所以整个小岛的周长是4×陆地数-2×相交数。
class Solution:
def islandPerimeter(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
M, N = len(grid), len(grid[0])
counts = 0
neighbors = 0
for i in range(M):
for j in range(N):
if grid[i][j] == 1:
counts += 1
if i < M - 1:
if grid[i + 1][j] == 1:
neighbors += 1
if j < N - 1:
if grid[i][j + 1] == 1:
neighbors += 1
return 4 * counts - 2 * neighbors
参考资料
https://leetcode.com/problems/island-perimeter/discuss/95001/clear-and-easy-java-solution
日期
2018 年 11 月 8 日 —— 项目进展缓慢
【LeetCode】463. Island Perimeter 解题报告(Python)的更多相关文章
- LeetCode 463 Island Perimeter 解题报告
题目要求 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 rep ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 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 ...
- [LeetCode] 463. Island Perimeter 岛的周长
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- LeetCode 463. Island Perimeter (岛的周长)
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- 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 ...
- 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 ...
- 3. leetcode 463 Island Perimeter
思路:设原始周长为4*节点数,每当出现一次相邻的情况,原始周长会减2.
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
随机推荐
- mysql 计算日期为当年第几季度
select T21620.日期 as F21634, QUARTER('98-04-01') as quarter #返回日期是一年的第几个季度 - ...
- php导出pdf,dompdf中文字体乱码解决办法(特别是代码迁移引起的乱码)
dompdf\lib\fonts\dompdf_font_family_cache.php记住这个文件里面存放的是字体生成的缓存,迁移时如果覆盖了这个文件会导致乱码而且很难找到出错的地方,相信我... ...
- C语言中的字节对齐
下面这个篇博客讲解很好 http://blog.csdn.net/meegomeego/article/details/9393783 总的来看分三类: 1. 不加 #pragma pack(n)伪指 ...
- MapReduce01 概述
MapReduce 概述 目录 MapReduce 概述 1.定义 2.优缺点 优点 缺点 3.MapReduce核心思想 4.MapReduce进程 5.官方 WordCount 源码 6.常用数据 ...
- 日常Java 2021/9/28
字符串反转 package m; public class m { public static void main(String[] args) { //定义一个字符串 String str = &q ...
- 如何删除苹果电脑垃圾文件-7个高级技巧释放大量苹果Mac
硬盘空间用尽是一件很让人头疼的事情,尤其是MacBook Air等设备上的固态硬盘可用的储存空间很少.下面[微IT]为大家介绍7个高级技巧来释放大量的硬盘空间,当然这些高级技巧更改了系统功能和文件,必 ...
- 图形学3D渲染管线学习
图形学3D渲染管线 DX和OpenGL左右手坐标系不同,会有一些差距,得出的矩阵会不一样; OpenGL的投影平面不是视景体的近截面: 顶点(vertexs) 顶点坐标,颜色,法线,纹理坐标(UV), ...
- 强化学习实战 | 表格型Q-Learning玩井字棋(二)
在 强化学习实战 | 表格型Q-Learning玩井字棋(一)中,我们构建了以Game() 和 Agent() 类为基础的框架,本篇我们要让agent不断对弈,维护Q表格,提升棋力.那么我们先来盘算一 ...
- Angular @Input讲解及用法
1.什么是@input @input的作用是定义模块输入,是用来让父级组件向子组件传递内容. 2.@input用法 首先在子组件中将需要传递给父组件的变量用@input()修饰 需要在子组件ts文件i ...
- oracle异常处理——ORA-01000:超出打开游标最大数
oracle异常处理--ORA-01000:超出打开游标最大数https://www.cnblogs.com/zhaosj/p/4309352.htmlhttps://blog.csdn.net/u0 ...