[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 ...
随机推荐
- STL_算法_05_集合算法
◆ 常用的集合算法: 1. 1.1.第6讲 PPT.40 ◆ set_union() : 构造一个有序序列,包含两个有序序列的并集. 1.2.第6讲 PPT.40 ◆ set_intersectio ...
- C语言专题-基本数据类和占位符
C语言中常用的几种基本数据类型有 基本数据类型的长度 unsigned unsigned unsigned unsigned float没有unsigned double没有unsigned 占位符的 ...
- HTTP请求GET/POST查看工具
当你有一个http的get请求需要知道结果,可以直接在浏览器上输入,然后等待查看结果. 那如果是一个post请求呢?推荐使用一个国外工具Send HTTP Tool. 传送门:http:/ ...
- Codeforces 534B - Covered Path
534B - Covered Path 思路:贪心,每一秒取尽可能大并且可以达到的速度. 画张图吧,不解释了: 代码: #include<bits/stdc++.h> using name ...
- m_Orchestrate learning system---三十四、使用重定义了$的插件的时候最容易出现的问题是什么
m_Orchestrate learning system---三十四.使用重定义了$的插件的时候最容易出现的问题是什么 一.总结 一句话总结:如下面这段代码,定义了$的值,还是会习惯性的把$当成jQ ...
- 用EL時(el-api.jar,el-ri.jar ),要設isELIgnored="false"
用EL時(el-api.jar,el-ri.jar ),要設isELIgnored="false" 否则jstl标签不显示. 加上 <%@page isELIgnored="false ...
- Style、ControlTemplate 和 DataTemplate 触发器
本文摘要: 1:属性触发器: 2:数据触发器: 3:事件触发器: Style.ControlTemplate 和 DataTemplate 都有触发器集合. 属性触发器只检查W ...
- IntelliJ IDEA 进行多线程调试
idea的断点有不同的模式,只有当Thread模式下才能调试多线程 断点设置步骤: 1.在断点上右键 2.选择Thread,然后点Done(建议选择Thread后点击make default把 ...
- Confluence 6 使用 LDAP 授权连接一个内部目录概述
你可以为你的 Confluence 连接 LDAP 服务器使用使用委托认证.这个意思是 Confluence 将会设置一个内部目录,这个目录仅被用来处理 LDAP 的授权. 这个设置将会为尝试登录系统 ...
- failed to load response data
当需要根据后台传回地址跳转页面时 即使使用preserve log 可以查看上一个页面获取地址请求,但是此时请求返回值为failed to load response data 当关闭页面跳转可以查看 ...