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. Base64原理解析

    一. Base64编码由来 为什么会有Base64编码呢?因为有些网络传送渠道并不支持所有的字节,例如传统的邮件只支持可见字符的传送,像ASCII码的控制字符就 不能通过邮件传送.这样用途就受到了很大 ...

  2. windows多线程编程

    进程共同实现某个任务或者共享计算机资源, 它们之间存在两种关系: 1.同步关系, 指为了完成任务的进程之间, 因为需要在某些位置协调它们的执行顺序而等待, 传递消息产生的制约关系. 2.互斥关系, 进 ...

  3. 小公司0成本基于Pythony的单元\GUI\Web自动化\性能的几个开源软件测试工具

    以下是当前流行的几款适合小公司0成本的几个开源软件测试解决方案: 1.单元测试 a.unittest :Python自带的单元测试框架 b.pyunit:Junit的Python版本 2.使用Pyho ...

  4. UVA445

    测试了很多数据都没问题,但是就是一直WA... #include<stdio.h> #include<string.h> int main(){ ]; int n; while ...

  5. note

    John的博客 http://blog.sina.com.cn/chinatownjohn 剑4-11真题+新东方pdf,王陆语料库(听力)+顾家北手把手教你写剑9版(写作)+人人雅思哥记忆卡(口语) ...

  6. windows下mysql数据库定时备份。

    注意:看本教程先必须会windows自带的"任务计划程序". 首先创建一个bat后缀的文件我的是timerExecutePhp.bat文件 timerExecutePhp.bat ...

  7. myql Connect

    mysql折腾笔记 2014-01-05 10:58 经常吹嘘自己玩过各种数据库. redis, mysql, sqlite, mongodb..常用数据库都不在话下,不料今天却在远程连接mysql上 ...

  8. nginx本地转发

    在conf文件下找到nginx.conf配置文件:添加如下代码:

  9. 12月18日Smarty文件缓存

    缓存 做缓存的目的是为了让程序运行起来更加迅速.因为如果程序访问数据库时数据量较大,执行起来会比较慢.而且每一次刷新页面都会访问依稀数据库,然后再把数据显示在页面上. 设置缓存也有一个缺点,那就是缓存 ...

  10. PHP XML和数组互相转换

    //数组转XML function arrayToXml($arr) { $xml = "<xml>"; foreach ($arr as $key=>$val) ...