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的更多相关文章

  1. 463. Island Perimeter - LeetCode

    Question 463. Island Perimeter Solution 题目大意:给出一个二维数组1表示陆地0表示海,求陆地的周长 思路: 重新构造一张地图grid2即一个二维数组,比原数组大 ...

  2. 【LeetCode】463. Island Perimeter

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...

  3. LeetCode 463 Island Perimeter 解题报告

    题目要求 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 rep ...

  4. 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 ...

  5. [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 ...

  6. 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 ...

  7. 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 ...

  8. 463. Island Perimeter岛屿周长

    [抄题]: You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 re ...

  9. 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 ...

随机推荐

  1. call和apply的区别

    call和apply都属于function prototype的一个方法. 定义:调用一个方法的对象,以另一个对象替换当前对象. 相同点:两个方法产生的作用是一样的. 不同点:方法传递的参数不同. c ...

  2. ActiveMQ安全配置

    1.activeMQ管理后台用户名.密码的设置 管理后台地址http://localhost:8161/admin 默认用户名密码admin admin 端口默认是8161,且服务采用的是服务器,所以 ...

  3. 跟着《beginning jquery》学写slider插件并借助自定义事件改进它

    <beginning jquery>是一本很不错的学习jquery的书,作者的讲解深入浅出,很适合初学者,在最后一章里面,作者把前面所有的点结合起来完成了一个轮播图的jquery插件.实现 ...

  4. Android技术分享收集

    Android高工必备技能! 我的 Android 开发实战经验总结 微信Android客户端架构演进之路 微信Android版智能心跳方案 流量优化: WebP 探寻之路 HTTP 协议缓存机制详解 ...

  5. 在Arcscene绘制管线三维横断面(AE绘制三维点阵文字)

    根据数据信息动态生成三维管线及横断面表格.效果图如下: 在获取信息后,直接构造点阵进行文字绘制即可. 绘制IElement代码: /// <summary> /// 绘制三维文字 /// ...

  6. Python环境配置安装

    2016年12月20日14:15:23 -------------- 参考菜鸟教程: Python 环境搭建 | 菜鸟教程  http://www.runoob.com/python/python-i ...

  7. 【Beta】Scrum08

    Info 由于雾霾严重,拍摄的照片笼罩了一层薄薄的白色. 时间:2016.12.21 21:35 时长:25min 地点:大运村1号公寓5楼楼道 类型:日常Scrum会议 NXT:2016.12.23 ...

  8. xpath 学习一: 节点

    xpath 中,有七种类型的节点: 元素.属性.文本.命名空间.处理指令.注释.以及根节点 树的根成为文档节点或者根节点. 节点关系: Parent, Children, sibling(同胞), A ...

  9. 借助Html制作渐变的网页背景颜色

    借助Html制作渐变的网页背景颜色 <html> <head> <title>制作渐变背景</title> <meta http-equiv=&q ...

  10. 记一次WinForm程序中主进程打开子进程并传递参数的操作过程(进程间传递参数)

    目标:想在WinForm程序之间传递参数.以便子进程作出相应的处理. 一种错误的方法 父进程的主程序: ProcessStartInfo psi = new ProcessStartInfo(); p ...