463. Island Perimeter
https://leetcode.com/problems/island-perimeter/
在一个N×N的矩阵中,N<100,1代表岛,0代表海,岛内没有海,求岛的周长
[[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:

由正方形组成的不规则图形的周长和正方形个数有什么关系?
这个就是这题的核心
发散一下平移的思想,多一个右邻居,多两条边,多一个下邻居,多两条边(当然你也可以统计左上)
公式就是“周长=正方形数量 * 4 - 邻居数量 * 2”
class Solution(object):
def islandPerimeter(self, grid):
island, neighbor = 0, 0
for i in xrange(0, len(grid)):
for j in xrange(0, len(grid[i])):
if grid[i][j] == 1:
island += 1
if i < len(grid) - 1 and grid[i + 1][j] == 1: # down neighbour
neighbor += 1
if j < len(grid[i]) - 1 and grid[i][j + 1] == 1: # right neighbour
neighbor += 1
return island * 4 - neighbor * 2
463. Island Perimeter的更多相关文章
- 463. Island Perimeter - LeetCode
Question 463. Island Perimeter Solution 题目大意:给出一个二维数组1表示陆地0表示海,求陆地的周长 思路: 重新构造一张地图grid2即一个二维数组,比原数组大 ...
- 【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&Python] Problem 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 - O(MN)- (C++) - 解题报告
原题 原题链接 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 ...
- 463. Island Perimeter Add to List
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- 463. Island Perimeter岛屿周长
[抄题]: You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 re ...
- 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 ...
随机推荐
- JSONP跨域操作
JSP <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3. ...
- 织梦多语言站点,{dede:include filename=''/}引入问题
织梦模板include插入非模板目录文件出现"无法在这个位置找到"错误的解决办法 以下是dede V55_UTF8 查dede include标签手册 (3) include 引入 ...
- neo4j关闭和开启密码访问权限
本例:neo4j-enterprise-2.3.1版本 neo4j默认安装是开启访问密码验证 可以发现,在conf/下的neo4j-server.properties配置文件 # Require (o ...
- MSSQL的表锁
DECLARE @PlanId INT; BEGIN TRAN; INSERT INTO TbName(col,col2) VALUES ('sss','2016/11/8 18:25:12'); S ...
- 附加属性出现Failed to assign to property的问题
找了半天资料,最后发现把保护附加属性的类加上public就行了
- php class
一个类可以包含有属于自己的常量,变量(称为"属性")以及函数(称为"方法"). $ 变量--专业术语上称它为"属性". function ...
- mybatis源码分析:
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Helvetica Neue"; color: #e4af0a } p. ...
- 自己写的服务出现"服务没有及时响应启动或控制请求 1053" 错误
自己写了一个服务,安装到电脑上后 启动时发现报"服务没有及时响应启动或控制请求 1053" 这个错误 在网上找了一些方法,都没有解决 后来,看了下,原来有个写文件的方法读取文件没有 ...
- keylogger
import pyHookimport sysimport pythoncomimport loggingfile_log = 'C:\\important\\log.txt'def OnKeyboa ...
- 面向对象继承 (for in 原型链查找属性)
window.onload=function(){ new Preson('liujian','男').show(); new Work('liujian','男','工人').show(); new ...