leetcode算法:Island Perimeter
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的更多相关文章
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 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 解题报告
题目要求 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 rep ...
- 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 - 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(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算法题-Island Perimeter(Java实现)
这是悦乐书的第238次更新,第251篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第105题(顺位题号是463).您将获得一个二维整数网格形式的地图,其中1代表土地,0代 ...
- 463. Island Perimeter - LeetCode
Question 463. Island Perimeter Solution 题目大意:给出一个二维数组1表示陆地0表示海,求陆地的周长 思路: 重新构造一张地图grid2即一个二维数组,比原数组大 ...
随机推荐
- postgresql和oracle数据库对比
SQL执行计划干预 从使用postgresql来看,想要改变执行计划只能通过対表进行分析,不能通过添加hint的方式来改变执行计划: oracle不仅可以通过对表进行收集统计来改变执行计划,而且很重要 ...
- The POM for ... is missing, no dependency information available
今天在重温淘淘商城的项目,准备用idea重写次,换个bootstrap的前端框架,但是在用idea构建maven项目后编译时却报错了: 经再三确认,common工程自身并没有任何问题,引用这个工程的地 ...
- 解决Centos7本机时间与实际时间相差8小时
# timedatectl Local -- :: CST Universal -- :: UTC #相差8小时 RTC -- :: Time zone: Asia/Shanghai (CST, +) ...
- 修改UI及盒子启动及部分后续操作
服务器操作: 1.配置nfs: # vim /etc/exports /home/nfsroot *(insecure,rw,no_root_squash) mkdir -pv /home/nfsro ...
- 变量对象VO与活动对象AO
变量对象VO 变量对象VO是与执行上下文相关的特殊对象,用来存储上下文的函数声明,函数形参和变量.在global全局上下文中,变量对象也是全局对象自身,在函数上下文中,变量对象被表示为活动对象AO. ...
- Vue之九数据劫持实现MVVM的数据双向绑定
vue是通过数据劫持的方式来做数据绑定的,其中最核心的方法便是通过Object.defineProperty()来实现对属性的劫持,达到监听数据变动的目的. 如果不熟悉defineProperty,猛 ...
- 【Django】 初步学习
这个系列(或者成不了一个系列..)预计会全程参考Vamei様的Django系列,膜一发.说句题外话,其实更加崇拜像Vamei那样的能够玩转生活.各个领域都能取得不小成就的人. [Django] ■ 概 ...
- Algorithm --> 全排列
1.算法简述 简单地说:全排列就是从第一个数字起每个数分别与它后面的数字交换. E.g:E = (a , b , c),则 prem(E)= a.perm(b,c)+ b.perm(a,c)+ c.p ...
- selenium2自动化测试学习笔记(四)
今天是学习selenium2第四天.总结下今天的学习成果,自动登录网易邮箱并写信发送邮件. 知识点or坑点: 1.模块化编写测试模块(类似java里的抽象方法,js的函数编写) from 包名 imp ...
- centos7上安装ffmpeg
FFmpeg介绍 FFmpeg是一个开源免费跨平台的视频和音频流方案,属于自由软件,采用LGPL或GPL许可证(依据你选择的组件).它提供了录制.转换以及流化音视频的完整解决方案.它包含了非常先进的音 ...