LeetCode 463 Island Perimeter 解题报告

题目要求

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.

题目分析及思路

题目给出二维整数数组,1代表土地,0代表水域。要求土地互相连接只能形成一个岛屿,然后被水域包围,内部不能含有水域。最后返回这个岛屿的周长。要计算岛屿的周长,其实可以将每个土地的周长求和,再减去邻接的个数的两倍。

python代码

class Solution:

def islandPerimeter(self, grid: List[List[int]]) -> int:

lands, neighbors = 0, 0

rows = len(grid)

cols = len(grid[0])

for r in range(rows):

for c in range(cols):

if grid[r][c] == 1:

lands += 1

if r < rows - 1:

if grid[r+1][c] == 1:

neighbors += 1

if c < cols - 1:

if grid[r][c+1] == 1:

neighbors += 1

return 4 * lands - 2 * neighbors

原文地址:https://www.cnblogs.com/yao1996/p/10425388.html

时间: 2024-10-08 16:00:59

LeetCode 463 Island Perimeter 解题报告的相关文章

Leetcode 463. Island Perimeter JAVA语言

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 isla

LeetCode: 463 Island Perimeter(easy)

题目: 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

[LeetCode] 463. Island Perimeter 岛的周长

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 isla

463. Island Perimeter - LeetCode

Question 463. Island Perimeter Solution 题目大意:给出一个二维数组1表示陆地0表示海,求陆地的周长 思路: 重新构造一张地图grid2即一个二维数组,比原数组大一圈,即长宽都大2 一个点在原地图坐标是(i,j),那么在重新构造的坐标就是(i+1,j+1) 遍历原地图,如果一是陆地,就遍历这个点的周围是否是海,如果是海线周长就加1 Java实现: public int islandPerimeter(int[][] grid) { int total = 0

LeetCode: Pascal&#39;s Triangle 解题报告

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] SOLUTION 1:很easy的题.注意记得把List加到ret中.比较简单,每一行的每一个元素有这个规律:1. 左右2边的是1.i, j 表示行,列坐标.2.

[LeetCode]Longest Valid Parentheses, 解题报告

题目 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example i

【LeetCode】Insert Interval 解题报告

[题目] Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and m

LeetCode: Unique Paths II 解题报告

Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty spac

【LeetCode】Symmetric Tree 解题报告

Symmetric Tree 解题报告 [LeetCode] https://leetcode.com/problems/symmetric-tree/ Total Accepted: 106639 Total Submissions: 313969 Difficulty: Easy Question Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For