-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
36 lines (31 loc) · 859 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* 岛屿数量
* @see https://leetcode-cn.com/problems/number-of-islands/
*/
function numIslands(grid: string[][]): number {
const m = grid.length
const n = grid[0].length
const used = Array.from({ length: m }, () => new Array(n).fill(false))
let result = 0
for (let y = 0; y < m; y++) {
for (let x = 0; x < n; x++) {
if (grid[y][x] === "1" && used[y][x] === false) {
dfs(y, x)
result++
}
}
}
function dfs(y: number, x: number) {
if (y < 0 || x < 0 || y >= m || x >= n || used[y][x]) {
return
}
if (grid[y][x] !== '1') return
used[y][x] = true
//如果为1进行上下左右查找
dfs(y - 1, x)
dfs(y + 1, x)
dfs(y, x - 1)
dfs(y, x + 1)
}
return result
};