【LeetCode】463. Island Perimeter 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/island-perimeter/description/
题目描述
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water.
Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).
The island doesn’t have “lakes” (water inside that isn’t connected to the water around the island). One cell is a square with side length 1. The grid is rectangular, width and height don’t exceed 100. Determine the perimeter of the island.
Example:
Input:
[[0,1,0,0],
[1,1,1,0],
[0,1,0,0],
[1,1,0,0]]
Output: 16
Explanation: The perimeter is the 16 yellow stripes in the image below:
题目大意
给了一个二维数组表示的一张地图,如果某个位置是1,那么表示的是陆地,位置是0表示的是海洋。已知陆地连在一起的话会构成一个小岛,而且这个地图里面只有一个小岛。求小岛的周长。
解题方法
减去相交部分
直接求解好像很难,可以使用一个简单的方法:每个位置的周长是4,如果和另一个陆地相连,那么这两个陆地周长和减去2.所以整个小岛的周长是4×陆地数-2×相交数。
class Solution:
def islandPerimeter(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
M, N = len(grid), len(grid[0])
counts = 0
neighbors = 0
for i in range(M):
for j in range(N):
if grid[i][j] == 1:
counts += 1
if i < M - 1:
if grid[i + 1][j] == 1:
neighbors += 1
if j < N - 1:
if grid[i][j + 1] == 1:
neighbors += 1
return 4 * counts - 2 * neighbors
参考资料
https://leetcode.com/problems/island-perimeter/discuss/95001/clear-and-easy-java-solution
日期
2018 年 11 月 8 日 —— 项目进展缓慢
【LeetCode】463. Island Perimeter 解题报告(Python)的更多相关文章
- 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】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 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 ...
- [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 represen ...
- 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: 463 Island Perimeter(easy)
题目: You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 repr ...
- 3. leetcode 463 Island Perimeter
思路:设原始周长为4*节点数,每当出现一次相邻的情况,原始周长会减2.
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
随机推荐
- R语言与医学统计图形-【27】ggplot2图形组合、字体、保存
ggplot2绘图系统--图形组合.字体选择.保存输出 1.图形组合 一页多图在基础包中利用par和layout函数来切分画布. ggplot2是先铺好网格背景,再进行绘图,所以要通过切分网格背景来实 ...
- Angular @ViewChild,Angular 中的 dom 操作
Angular 中的 dom 操作(原生 js) ngAfterViewInit(){ var boxDom:any=document.getElementById('box'); boxDom.st ...
- linux允许直接以root身份ssh登录
1. sudo su - 2. vim /etc/ssh/sshd_config 3. let "PermitRootLogin" equal yes 4. :wq 5. serv ...
- Linux网络管理(一)之配置主机名与域名
Linux网络管理(一)之配置主机名与域名参考自:[1]修改主机名(/etc/hostname和/etc/hosts区别) https://blog.csdn.net/shmily_lsl/artic ...
- 转Android service 启动篇之 startForegroundService
本文转自:https://blog.csdn.net/shift_wwx/article/details/82496447 前言: 在官方文档 Android 8.0 行为变更 中有这样一段话: An ...
- Java中特殊的类——包装类
Java中特殊的类--包装类 包装类就是将基本数据类型封装在类中. 1.包装类 (1)自定义包装类 将基本数据类型包装成一个类对象的本质就是使用Object进行接收处理. 此时IntDemo类就是in ...
- centos7 自动同步时间
rm -rf /etc/localtime ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime vim /etc/sysconfig/cloc ...
- vue-cli 如何配置assetsPublicPath; vue.config.js如何更改assetsPublicPath配置;
问题: vue项目完成打包上线的时候遇到静态资源找不到的问题,网上很多解决办法都是基于vue-cli 2.x 来解决的,但从vue-cli 3.0以后,便舍弃了配置文件夹(便没有了config这个文件 ...
- When do we use Initializer List in C++?
Initializer List is used to initialize data members of a class. The list of members to be initialize ...
- typora使用快捷键
1. Ctrl+/ 切换源码模式2. ```css 选择语言 回车.4. `code` ctrl+shit+` 5. # 1号标题 ctrl+1 ### 3号标题 ctrl+3 ######6号标题 ...