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 ...
随机推荐
- 【BZOJ 1016】【JSOI 2008】最小生成树计数
http://www.lydsy.com/JudgeOnline/problem.php?id=1016 统计每一个边权在最小生成树中使用的次数,这个次数在任何一个最小生成树中都是固定的(归纳证明). ...
- 【BZOJ 3445】【Usaco2014 Feb】Roadblock
http://www.lydsy.com/JudgeOnline/problem.php?id=3445 加倍的边一定在最短路上(否则继续走最短路). 最短路长度是O(n)的,暴力扫最短路上的每条边, ...
- thinkcmf无法使用config.html中的配置量
在模版中引入 <tc_include file=":config" />
- 冰冻三尺非一日之寒--jQuery
第十七章 jQuery http://jquery.cuishifeng.cn/ 一.过滤选择器: 目的:处理更复杂的选择,是jQuery自定义的,不是CSS3中的选择器. ...
- Django知识点整理
什么是web框架 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单地说,就是你用别人搭建好的舞台来做表演. web应用 访 ...
- Android源码——AsynTask
AsyncTask<Params, Progress, Result>中三个参数为: Params 输入数据 Progress 过程数据 Result ...
- Nginx配置性能优化
大多数的Nginx安装指南告诉你如下基础知识--通过apt-get安装,修改这里或那里的几行配置,好了,你已经有了一个Web服务器了.而且,在大多数情况下,一个常规安装的nginx对你的网站来说已经能 ...
- 网页访问全过程剖析[转].md
本文转载自:http://www.cnblogs.com/wenanry/archive/2010/02/25/1673368.html 原文翻译自:http://igoro.com/archive/ ...
- win7里边使用telnet命令为什么提示telnet不是内部或外部命令,也不是可运行的程序或批处理文件
Win7默认没有安装telnet功能,所以你直接用telnet命令是用不了的:你可以去“控制面板”-->“程序”(在左下角)--->“打开或关闭Windows功能”,勾上“telnet客户 ...
- RF 基本方法
1. Select From List id=sourceConn oracle_source 从下拉框选取值. 2. Select Radio button name value 选择单选框.也可 ...