[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 ...
随机推荐
- Qt_2D_画图教程
1. ZC: 看点:相同的API,QPainter.QPainterDevice和QPainterEngine这3个类 Qt学习之2D绘图(画刷和画笔) http://blog.csdn.net/lp ...
- 《剑指offer》第二十二题(链表中倒数第k个结点)
// 面试题22:链表中倒数第k个结点 // 题目:输入一个链表,输出该链表中倒数第k个结点.为了符合大多数人的习惯, // 本题从1开始计数,即链表的尾结点是倒数第1个结点.例如一个链表有6个结点, ...
- jq的attr()与prop()之间区别
1.attr() 一直存在,prop() 仅存在于 jq-1.6 及其之后 2.新版本jq使用细节: 2.1 自定义添加至dom节点的属性,用attr获取 2.2 表单类checked.selecte ...
- zzuli2130卡时bfs题
https://acm.zzuli.edu.cn/zzuliacm/problem.php?id=2130 2130: hipercijevi Time Limit: 1 Sec Memory Li ...
- 3n+1问题中的几个小的注意点
3038 3n+1问题 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 白银 Silver 题解 题目描述 Description 3n+1问题是一个简单有趣而又没有 ...
- 使用a标签实现软件下载及下载量统计
通常最简单的软件下载就是采用如下方式: <a id="welcomeMiddleBtn" href="${basePath}/files/client/instal ...
- notepad++插件安装
notepad安装目录的 plugins 下重启 notepad.exe程序即可 插件下载地址 : https://sourceforge.net/projects/npp-plugins/fil ...
- httpclient cookie使用介绍
COOKIE的处理 session的保持是通过cookie来维持的,所以如果用户有勾选X天免登陆,这个session就X天内一直有效,就是通过这个cookie来维持. 如果没有选中x天免登陆,基本上就 ...
- linux make virtual memory more efficient three components
Page Cache This is used to speed up access to images and data on disk. As pages are read into memory ...
- pip install flask 安装失败
地址:http://www.zhihu.com/question/21492455