[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 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:
We can consider a bigger grid:
- 0 0 0 0 0 0
0[[0,1,0,0] 0- 0 [1,1,1,0] 0
- 0 [0,1,0,0] 0
- 0 [1,1,0,0]]0
0 0 0 0 0 0- We can just check the 0s in the grid. If there is a 1 next to it, we add one to the answer.
- class Solution:
- def islandPerimeter(self, grid):
- """
- :type grid: List[List[int]]
- :rtype: int
- """
- m=len(grid)
- n=len(grid[0])
- ans=0
- for row in range(-1,m+1):
- for column in range(-1,n+1):
- if row==-1 or column==-1 or row==m or column==n or grid[row][column]==0:
- for d in [(1,0),(0,1),(-1,0),(0,-1)]:
- newR,newC=row+d[0],column+d[1]
- if 0<=newR<m and 0<=newC<n and grid[newR][newC]==1:
- ans+=1
- return ans
[LeetCode&Python] Problem 463. Island Perimeter的更多相关文章
- 463. Island Perimeter - LeetCode
Question 463. Island Perimeter Solution 题目大意:给出一个二维数组1表示陆地0表示海,求陆地的周长 思路: 重新构造一张地图grid2即一个二维数组,比原数组大 ...
- 【LeetCode】463. Island Perimeter 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 减去相交部分 参考资料 日期 题目地址:https: ...
- [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 (岛的周长)
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 - 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 ...
随机推荐
- php json josn_decode()返回的是对像,如何把对像转成数组
php json josn_decode()返回的是对像,如何把对像转成数组 a.php传值页面,使用 json_encode($array)对数组进行加密码. b.php页面在接收a.php传过来的 ...
- border可以这样给控件加边框
<Border.BorderBrush> <SolidColorBrush Color="Red" Opacity="0" / ...
- English trip V1 - 7.My dream car 我梦想的车 Teacher:Lamb Key: famous for
中华In this lesson you will learn to describe an object(目标). 课上内容(Lesson) famous for 以…著称,闻名 国家(名词) ...
- gleez开发环境搭建
一.虚拟主机目录配置 1.配置apache服务器 Apache是常用的web服务器,即常见的用来处理http协议,处理网页的. Apache的配置文件都存放在/etc/apache2/目录,这里有很多 ...
- Oracle11g温习-第六章:控制文件
2013年4月27日 星期六 10:33 .控制文件的功能和特点 1) [定义数据库当前物理状态] 2) [维护数据的一致性] 如果控制文件中的检查点与数据文件中的一致,则说明数据一致,可以启动到 ...
- iframe刷新父页面
iframe页面是内嵌到父页面的,当点击iframe页面的服务器控件时,默认只刷新iframe页面,父页面是不会刷新的.若想刷新父页面,可以使用js来实现,如 1. parent.location.r ...
- OC 设计模式
设计模式 一种或几种被所有程序员广泛认同的,有某些特定功能,或者实现某些特殊作用的编码格式 单例模式 键值编码(KVC) 键值观察(KVO) 观察者模式() 工厂模式(工厂方法) ps:MVC &am ...
- popen strtok 函数的使用
FILE * popen ( const char * command , const char * type ); int pclose ( FILE * stream ); type 参数只能 ...
- POJ 1008 简单模拟题
e.... 虽然这是一道灰常简单的模拟题.但是米做的时候没有读懂第二个日历的计时方法.然后捏.敲完之后华丽的WA了进一个点.坑点就在一年的最后一天你是该输出本年的.e ...但是我好想并没有..看di ...
- CAS-登出配置
该图 当一个web 浏览器登录到应用服务器时,应用服务器(application)会监测用户的session,如果没有session,则应用服务器会把url跳转到CAS server上.要求 用户登录 ...