[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 ...
随机推荐
- SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件
原文链接 我们这一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池 ...
- mysql 问题 Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdb
异常错误:Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.c ...
- GetContent
Sub GetContent(ByVal URL As String, ByVal SheetName As String) Dim strText As String Dim i As Long D ...
- Linux Used内存到底哪里去了?
原创文章,转载请注明: 转载自系统技术非业余研究 本文链接地址: Linux Used内存到底哪里去了? 前几天 纯上 同学问了一个问题: 我ps aux看到的RSS内存只有不到30M,但是free看 ...
- PC端,移动端分离,如何结合??
<script type="text/javascript"> function mobile_device_detect(url) { var thisOS = na ...
- mysql语句查询时间检测
explain的使用 1.首先我们是要登入你的mysql的,然后选择数据库输入:use 你要选择的库名 2执行语句 eg: explain SELECT * FROM wish_orders1412 ...
- 牛客网——E求最值
链接:https://www.nowcoder.com/acm/contest/59/E来源:牛客网 题目描述 给你一个长为n的序列a 定义f(i,j)=(i-j)2+g(i,j)2 g是这样的一个函 ...
- HDOJ1007
/** 最近点对问题,时间复杂度为O(n*logn*logn) */ #include <iostream> #include <cstdio> #include <cs ...
- jQuery旋转插件—rotate-摘自网友
jQuery旋转插件—rotate 时间:2013年01月03日作者:愚人码头查看次数:5,660 views评论次数:6条评论 网上发现一个很有意思的jQuery旋转插件,支持Internet Ex ...
- 快速切题 usaco ariprog
题目:给定3<=n<=25,m<250,求m及以内的两两平方和能否构成为n的等差数列 1 WA 没有注意到应该按照公差-首项的顺序排序 2 MLE 尝试使用桶,但是实际上那可能是分散 ...