Movatterモバイル変換


[0]ホーム

URL:


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

Commitb47495a

Browse files
committed
Update 0463. 岛屿的周长.md
1 parent8069239 commitb47495a

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

‎Solutions/0463. 岛屿的周长.md‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,38 @@
55

66
##题目大意
77

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

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

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

14-
要求:计算这个岛屿的周长。
14+
**要求**:计算这个岛屿的周长。
1515

16-
示例:下图周长为 16。
16+
**说明**
17+
18+
- $row == grid.length$。
19+
- $col == grid[i].length$。
20+
- $1 <= row, col <= 100$。
21+
- $grid[i][j]$ 为 $0$ 或 $1$。
22+
23+
**示例**
1724

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

27+
```Python
28+
输入:grid= [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
29+
输出:16
30+
解释:它的周长是上面图片中的16 个黄色的边
31+
32+
33+
输入:grid= [[1]]
34+
输出:4
35+
```
36+
2037
##解题思路
2138

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

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

32-
##代码
49+
###思路 1:代码
3350

3451
```Python
3552
classSolution:
@@ -63,6 +80,11 @@ class Solution:
6380
returnself.bfs(grid, rows, cols, row, col)
6481
```
6582

83+
###思路 1:复杂度分析
84+
85+
-**时间复杂度**:$O(n \times m)$,其中 $m$ 和 $n$ 分别为行数和列数。
86+
-**空间复杂度**:$O(n \times m)$。
87+
6688
##参考资料
6789

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp