Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
xjq7 committed Oct 8, 2024
1 parent 7193134 commit 07537c1
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions docs/Knowledge/Base.md
Original file line number Diff line number Diff line change
Expand Up @@ -936,5 +936,52 @@ djb2 是一个产生随机分布的的哈希函数
}

return Math.max(dp[n - 1][1], dp[n - 1][2])
```
## BFS
广度优先搜索
- :yellow_circle: [200. 岛屿数量](https://leetcode.cn/problems/number-of-islands/description/)
```Js
/**
* @param {character[][]} grid
* @return {number}
*/
var numIslands = function (grid) {
const m = grid.length, n = grid[0].length

let ans = 0

const rects = [[-1, 0], [1, 0], [0, -1], [0, 1]]

const bfs = (i, j) => {
grid[i][j] = '0'
rects.forEach((rect) => {
i += rect[0]
j += rect[1]
if (i >= 0 && j >= 0 && i < m && j < n && grid[i][j] === '1') {
grid[i][j] = '0'
bfs(i, j)
}
i -= rect[0]
j -= rect[1]
})

}

for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (grid[i][j] === '0') {
continue
}

ans++
bfs(i, j)
}
}

return ans
};
```

0 comments on commit 07537c1

Please sign in to comment.