Skip to content

Commit 8001c16

Browse files
author
sunzhishuai
committed
merge
2 parents 68b8f18 + 6f999dc commit 8001c16

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ exention -> exection (replace 'n' with 'c')
290290
exection -> execution (insert 'u')
291291
```
292292

293-
47. ***【Number of Islands】*** Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
293+
47. ***[Number of Islands](./doc/numIsLands/算法222.md)*** Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
294294
Example 1:
295295
```
296296
Input:
@@ -424,6 +424,7 @@ You can assume that you can always reach the last index.
424424
## 加密算法专题
425425
56. AES
426426
57. RSA
427+
<<<<<<< HEAD
427428

428429
## Leetcode 算法
429430

@@ -553,3 +554,5 @@ You can assume that you can always reach the last index.
553554
554555
> 本算法目录中Leetcode章节中的算法来自[leetcode](https://leetcode-cn.com/)。请勿商业使用!
555556
557+
=======
558+
>>>>>>> 6f999dc451351e16f3b0aba4cd586b395e9e8e85

doc/numIsLands/算法222.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#问题
1+
# 问题
22
给你一个由 '1'(陆地)和 '0'(水)组成的的二维网格,请你计算网格中岛屿的数量。岛屿总是被水包围,并且每座岛屿只能由水平方向或竖直方向上相邻的陆地连接形成。此外,你可以假设该网格的四条边均被水包围。
33

44

@@ -11,7 +11,7 @@
1111
![avatar](../../res/numIsLands/22222.png)
1212

1313

14-
#深度优先遍历DFS
14+
# 深度优先遍历DFS
1515

1616

1717
![avatar](../../res/numIsLands/dfs.png)
@@ -99,15 +99,15 @@ public class Solution {
9999
}
100100
}
101101
```
102-
##复杂度分析
102+
## 复杂度分析
103103
时间复杂度:O(MN)O(MN),其中 MM 和 NN 分别为行数和列数。
104104

105105
空间复杂度:O(MN)O(MN),在最坏情况下,整个网格均为陆地,深度优先搜索的深度达到 M NMN。
106106

107107

108108

109109

110-
#广度优先遍历 BFS
110+
# 广度优先遍历 BFS
111111

112112
我一开始在编写的时候,等到队列出队的时候才标记 “已经访问”,事实上,这种做法是错误的。因为如果不在刚刚入队列的时候标记 “已经访问”,相同的结点很可能会重复入队。
113113
![avatar](../../res/numIsLands/bfs.png)
@@ -201,15 +201,15 @@ public class Solution2 {
201201
}
202202
```
203203

204-
##复杂度分析
204+
## 复杂度分析
205205
时间复杂度:O(MN)O(MN),其中 MM 和 NN 分别为行数和列数。
206206

207207
空间复杂度:O(min(M, N))O(min(M,N)),在最坏情况下,整个网格均为陆地,队列的大小可以达到 min(M, N)min(M,N)。
208208

209209

210-
#使用并查集
210+
# 使用并查集
211211

212-
##并查集
212+
## 并查集
213213
在计算机科学中,并查集是一种树型的数据结构,用于处理一些不交集(Disjoint Sets)的合并及查询问题。有一个联合-查找算法(union-find algorithm)定义了两个用于此数据结构的操作:
214214

215215
Find:确定元素属于哪一个子集。它可以被用来确定两个元素是否属于同一子集。
@@ -219,12 +219,12 @@ Union:将两个子集合并成同一个集合。
219219

220220

221221

222-
###使用并查集解决本问题的思想很简单:
222+
### 使用并查集解决本问题的思想很简单:
223223

224224
1、如果当前是“陆地”,尝试与周围合并一下;
225225

226226
2、如果当前是“水域”,就把所有的“水域”合并在一起,为此,我设置了一个虚拟的结点,表示“所有的水域都和这个虚拟结点是连接的”。
227-
##注意
227+
## 注意
228228
最终岛屿的数量就是并查集中连通分量的数目。
229229

230230
把整个grid传入UnionFind计算count
@@ -296,7 +296,7 @@ class Solution {
296296
}
297297
}
298298
```
299-
##复杂度分析
299+
## 复杂度分析
300300
时间复杂度:O(MN * alpha(MN))O(MN∗α(MN)),其中 MM 和 NN 分别为行数和列数。注意当使用路径压缩(见 find 函数)和按秩合并(见数组 rank)实现并查集时,单次操作的时间复杂度为 alpha(MN)α(MN),其中 alpha(x)α(x) 为反阿克曼函数,当自变量 xx 的值在人类可观测的范围内(宇宙中粒子的数量)时,函数 alpha(x)α(x) 的值不会超过 55,因此也可以看成是常数时间复杂度。
301301

302302
空间复杂度:O(MN)O(MN),这是并查集需要使用的空间。

0 commit comments

Comments
 (0)