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 代表陆地 0代表海洋。相邻的1就是连着的陆地。
把每块陆地想成正方形,边长是1。
现在给定某个二维数组grid,我们来算出相连陆地的总周长。 思想就是遍历整个二位数组,对于每一个数,如果他是陆地 就看他的四周,有一个方向不是陆地 周长就加1 我的代码:
 class Solution(object):
def islandPerimeter(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
res = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
if i == 0 or grid[i-1][j] == 0:
res +=1
if i==len(grid)-1 or grid[i+1][j]==0:
res +=1
if j==0 or grid[i][j-1] ==0:
res +=1
if j==len(grid[0])-1 or grid[i][j+1]==0:
res += 1
return res if __name__ == '__main__':
s = Solution()
res = s.islandPerimeter( [
[0,1,0,0],
[1,1,1,0],
[0,1,0,0],
[1,1,0,0]
])
print(res)

leetcode算法:Island Perimeter的更多相关文章

  1. 【LeetCode】Island Perimeter 解题报告

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

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

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

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

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

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

  8. 3. leetcode 463 Island Perimeter

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

  9. LeetCode算法题-Island Perimeter(Java实现)

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

  10. 463. Island Perimeter - LeetCode

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

随机推荐

  1. 关于win8/win8.1系统不能调节亮度的解决办法

    有时候我们重装了win8系统之后开始可以调节亮度,但是再重新安装了显卡驱动之后发现不能调节亮度了,解决办法就是添加一项注册表. 下载地址:http://7xnarc.com1.z0.glb.cloud ...

  2. SAP OLE中常用的一些方法和属性

    1.ole中如何保存和退出. call method of sheetname = filepath # =. call method of applicationname 'quit'. 2.给sh ...

  3. C语言最后一次作业--总结报告

    1.当初你是如何做出选择计算机专业的决定的? 经过一个学期,你的看法改变了么,为什么? 你觉得计算机是你喜欢的领域吗,它是你擅长的领域吗? 为什么? 当时选择计算机专业,是基于自己的高考分数和想出省的 ...

  4. 用golang 实现一个代理池

    背景 写爬虫的时候总会遇到爬取速度过快而被封IP的情况,这个时候就需要使用代理了.在https://github.com/henson/ProxyPool 的启发下,决定自己实现一个代理池.项目已经开 ...

  5. Swift4--函数,自学笔记

    函数 函数名 描述函数功能,调用函数时使用. 定义和调用函数 func greetAgain(person: String) -> String { return "Hello aga ...

  6. 实现Windows数据绑定

    dataSet数据集   dataset驻留于内存临时存储数据简单的理解为一个临时数据库将数据源的数据保存在内存中独立于任何数据库创建dataset对象引入命名空间:system.Datadatase ...

  7. 20162311 实验二 Java面向对象程序设计 实验报告

    实验二 Java面向对象程序设计 实验内容 1. 初步掌握单元测试和TDD 2. 理解并掌握面向对象三要素:封装.继承.多态 3. 初步掌握UML建模 4. 熟悉S.O.L.I.D原则 5. 了解设计 ...

  8. 201621123062《java程序设计》第五周作业总结

    1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 关键词:interface.Comparable.comparator 1.2 尝试使用思维导图将这些关键词组织起来.注:思维导 ...

  9. 冲刺No.4

    Alpha冲刺第四天 站立式会议 项目进展 今日团队开始对项目的核心功能中的事务管理员模块与学生模块进行了编码,主要内容是对学生基本信息的增删改与事务管理员信息的增删改,这部分的内容是整个项目最基础的 ...

  10. Java的暑期作业

    Java暑期作业 一.<恶意>读书笔记 <恶意>是日本作家东野圭吾写的推理小说之一.看完后不禁为东野先生的奇特的写作手法以及书中所展现的人性的丑恶所震撼.我认为这本书相较< ...