作者: 负雪明烛
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)的更多相关文章

  1. LeetCode 463 Island Perimeter 解题报告

    题目要求 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 rep ...

  2. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  3. 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 ...

  4. [LeetCode] 463. Island Perimeter 岛的周长

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...

  5. LeetCode 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岛屿的周长 (C++)

    题目: You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 repr ...

  7. 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 ...

  8. 3. leetcode 463 Island Perimeter

    思路:设原始周长为4*节点数,每当出现一次相邻的情况,原始周长会减2.

  9. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

随机推荐

  1. docker_清华源国内的选择

    清华大学开源镜像官网:https://mirrors.tuna.tsinghua.edu.cn/ 前期: 在centos7 中的extras 源里面有docker 安装包,但是里面的安装包是比较旧的 ...

  2. 59. Divide Two Integers

    Divide Two Integers My Submissions QuestionEditorial Solution Total Accepted: 66073 Total Submission ...

  3. CSS3单行文本两端对齐

    CSS3实现单行文本两端对齐 p { height: 24px; text-align: justify; text-last-align: justify; } p::after { display ...

  4. 禁止点击、禁止button触发【c#】

    bts.Attributes["onclick"] = "return false; ";

  5. C#点击按钮添加标签

    <asp:Button ID="button1" runat="server" Text="创建" onclick="But ...

  6. jsp的动态包含和静态包含

    jsp的动态包含和静态包含 例如:提取一个公共的页面(top.jsp)到/WEB-INF/jsp/common/目录下 动态包含: 被包含的页面也会独立编译,生成字节码文件,一般包含页面信息频繁变化的 ...

  7. MapReduce06 MapReduce工作机制

    目录 5 MapReduce工作机制(重点) 5.1 MapTask工作机制 5.2 ReduceTask工作机制 5.3 ReduceTask并行度决定机制 手动设置ReduceTask数量 测试R ...

  8. Erda 系列 Meetup「成都站」携手SOFAStack 和你聊聊云原生基础设施建设那点事儿

    技术控快上车啦秋天的第一场活动一起来收获技术干货吧! 主题: 云原生基础设施建设的现在及未来时间: 2021 年 9 月 11 日 (周六) 13:30-17:00活动地点: 四川省成都市蚂蚁 C 空 ...

  9. ace

    ace An ace is a playing card, die or domino with a single pip. In the standard French deck, an ace h ...

  10. 【JavaWeb安全】RMI-Remote Method Invocator

    RMI-Remote Method Invocator 什么是RMI?RMI有什么用? RMI允许用户通过数据传输,调用远程方法,在远程服务器处理数据.例如将1,3传到远程服务器的加法运算器,加法运算 ...