https://leetcode.com/problems/island-perimeter/

在一个N×N的矩阵中,N<100,1代表岛,0代表海,岛内没有海,求岛的周长

  1. [[0,1,0,0],
  2. [1,1,1,0],
  3. [0,1,0,0],
  4. [1,1,0,0]]
  5.  
  6. Answer: 16
  7. Explanation: The perimeter is the 16 yellow stripes in the image below:

由正方形组成的不规则图形的周长和正方形个数有什么关系?

这个就是这题的核心

发散一下平移的思想,多一个右邻居,多两条边,多一个下邻居,多两条边(当然你也可以统计左上)

公式就是“周长=正方形数量 * 4 - 邻居数量 * 2”

  1. class Solution(object):
  2. def islandPerimeter(self, grid):
  3. island, neighbor = 0, 0
  4. for i in xrange(0, len(grid)):
  5. for j in xrange(0, len(grid[i])):
  6. if grid[i][j] == 1:
  7. island += 1
  8. if i < len(grid) - 1 and grid[i + 1][j] == 1: # down neighbour
  9. neighbor += 1
  10. if j < len(grid[i]) - 1 and grid[i][j + 1] == 1: # right neighbour
  11. neighbor += 1
  12. 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. 【BZOJ 1016】【JSOI 2008】最小生成树计数

    http://www.lydsy.com/JudgeOnline/problem.php?id=1016 统计每一个边权在最小生成树中使用的次数,这个次数在任何一个最小生成树中都是固定的(归纳证明). ...

  2. 【BZOJ 3445】【Usaco2014 Feb】Roadblock

    http://www.lydsy.com/JudgeOnline/problem.php?id=3445 加倍的边一定在最短路上(否则继续走最短路). 最短路长度是O(n)的,暴力扫最短路上的每条边, ...

  3. thinkcmf无法使用config.html中的配置量

    在模版中引入 <tc_include file=":config" />

  4. 冰冻三尺非一日之寒--jQuery

    第十七章     jQuery          http://jquery.cuishifeng.cn/ 一.过滤选择器: 目的:处理更复杂的选择,是jQuery自定义的,不是CSS3中的选择器. ...

  5. Django知识点整理

    什么是web框架 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单地说,就是你用别人搭建好的舞台来做表演. web应用 访 ...

  6. Android源码——AsynTask

    AsyncTask<Params, Progress, Result>中三个参数为: Params         输入数据 Progress       过程数据 Result     ...

  7. Nginx配置性能优化

    大多数的Nginx安装指南告诉你如下基础知识--通过apt-get安装,修改这里或那里的几行配置,好了,你已经有了一个Web服务器了.而且,在大多数情况下,一个常规安装的nginx对你的网站来说已经能 ...

  8. 网页访问全过程剖析[转].md

    本文转载自:http://www.cnblogs.com/wenanry/archive/2010/02/25/1673368.html 原文翻译自:http://igoro.com/archive/ ...

  9. win7里边使用telnet命令为什么提示telnet不是内部或外部命令,也不是可运行的程序或批处理文件

    Win7默认没有安装telnet功能,所以你直接用telnet命令是用不了的:你可以去“控制面板”-->“程序”(在左下角)--->“打开或关闭Windows功能”,勾上“telnet客户 ...

  10. RF 基本方法

    1. Select From List id=sourceConn oracle_source 从下拉框选取值. 2. Select Radio button  name value 选择单选框.也可 ...