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 ...
随机推荐
- Android Studio 常见异常解决办法
Error:Failed to crunch file D:\Englis_installation_directory\AndroidStudio\AndroidWorkSpace\YoukAndr ...
- java--面向对象---访问属性
<第一节> private--只能用于成员变量和成员函数(限制于类而不是对象),即只能在本类中访问被定义为private 的变量 public-- protected-- <第二节& ...
- RabbitMQ 集群之镜像同步
mirrored 在上个博文中讲到了如果做集群,那么集群是成功了,但是queue是如何存放的呢?消息又是怎么同步呢. 默认的,也就是什么也不配置,直接在某个节点中添加一个queue,那么它仅仅是属于这 ...
- mybatis generator.xml 配置 自动生成model,dao,mapping
generator.xml文件: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE gener ...
- 面向对象继承 (for in 原型链查找属性)
window.onload=function(){ new Preson('liujian','男').show(); new Work('liujian','男','工人').show(); new ...
- R语言获取数据类型信息的一些有用函数
向量.因子.时间序列x[i]: 矩阵.数据框x[i, j] x[i, ] x[, j]: 数组就是根据维度多打几个逗号而已x[i, j, k, -]: 列表要用双重中括号x[[i]]. 特殊的 ...
- React的井字过三关(1)
React的井字过三关(1) 本文系React官方教程的Tutorial: Intro To React的笔记.由笔者用ES5语法改写. 在本篇笔记中,尝试用React构建一个可交互的井字棋游戏. 开 ...
- webpack踩坑之路——构建基本的React+ES6项目
转自:http://www.cnblogs.com/ghost-xyx/p/5483464.html webpack是最近比较火的构建工具,搭配上同样比较火的ReacJS与ES6(ES2015)一定是 ...
- 显示oracle表的分区信息
显示分区表信息 显示数据库所有分区表的信息:DBA_PART_TABLES 显示当前用户可访问的所有分区表信息:ALL_PART_TABLES 显示当前用户所有分区表的信息:USER_PART_TAB ...
- hdu2211杀人游戏
Problem Description 不知道你是否玩过杀人游戏,这里的杀人游戏可没有法官,警察之类的人,只有土匪,现在已知有N个土匪站在一排,每个土匪都有一个编号,从1到N,每次杀人时给定一个K值, ...