Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] main from itcharge:main #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 5 commits into AlgorithmAndLeetCode:main from itcharge:main
Sep 9, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update 0463. 岛屿的周长.md
  • Loading branch information
itcharge committed Sep 9, 2022
commit b47495a0e28698551e18c9a9da32c5cc80aa1b06
32 changes: 27 additions & 5 deletions Solutions/0463. 岛屿的周长.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,38 @@

## 题目大意

给定一个 `row * col` 大小的二维网格地图 `grid` ,其中:`grid[i][j] = 1` 表示陆地,`grid[i][j] = 0` 表示水域。
**描述**:给定一个 `row * col` 大小的二维网格地图 `grid` ,其中:`grid[i][j] = 1` 表示陆地,`grid[i][j] = 0` 表示水域。

网格中的格子水平和垂直方向相连(对角线方向不相连)。整个网格被水完全包围,但其中恰好有一个岛屿(多个表示陆地的格子相连组成)。

岛屿内部中没有「湖」(指水域在岛屿内部且不和岛屿周围的水相连)。格子是边长为 1 的正方形。网格为长方形,且宽度和高度均不超过 100 。

要求:计算这个岛屿的周长。
**要求**:计算这个岛屿的周长。

示例:下图周长为 16。
**说明**:

- $row == grid.length$。
- $col == grid[i].length$。
- 1ドル <= row, col <= 100$。
- $grid[i][j]$ 为 0ドル$ 或 1ドル$。

**示例**:

![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/10/12/island.png)

```Python
输入:grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
输出:16
解释:它的周长是上面图片中的 16 个黄色的边


输入:grid = [[1]]
输出:4
```

## 解题思路

可以使用广度优先搜索求解。具体做法如下:
### 思路 1:广度优先搜索

1. 使用整形变量 `count` 存储周长,使用队列 `queue` 用于进行广度优先搜索。
2. 遍历一遍二维数组 `grid`,对 `grid[row][col] == 1` 的区域进行广度优先搜索。
Expand All @@ -29,7 +46,7 @@
6. 如果相邻区域 `grid[new_row][new_col] == 1`,则将其赋值为 `2`,并将坐标加入队列。
7. 继续执行 4 ~ 6 步,直到队列为空时返回 `count`。

##代码
### 思路 1:代码

```Python
class Solution:
Expand Down Expand Up @@ -63,6 +80,11 @@ class Solution:
return self.bfs(grid, rows, cols, row, col)
```

### 思路 1:复杂度分析

- **时间复杂度**:$O(n \times m),ドル其中 $m$ 和 $n$ 分别为行数和列数。
- **空间复杂度**:$O(n \times m)$。

## 参考资料

- 【题解】[Golang BFS 实现,性能比dfs要高 - 岛屿的周长 - 力扣](https://leetcode.cn/problems/island-perimeter/solution/golang-bfs-shi-xian-xing-neng-bi-dfsyao-nln2g/)

AltStyle によって変換されたページ (->オリジナル) /