[Java][LeetCode][BFS][DFS][Graph] Number Of Islands #200

SP Hou
Apr 25, 2022

--

DFS用遞迴的方式,BFS用Queue的方式。
歷遍所有1的位置,再藉由這個位置,將該位置的上下左右都蓋為0,一邊歷遍,一邊蓋0,要注意的點是:
1. 上下左右的位置必須在有matrix圍內:

x≥0 && x<grid.length && y≥0 && y<grid[0].length

2. 上下左右的值為1

grid[m][n]==’1'

DFS is with recursive, and BFS is with Queue.
Traversal all position of ‘1’. Dependent on the position, change the value of neighbors to ‘0’. Keeping in mind:
1. Positions of neighbors are inside the edge of the matrix.

x≥0 && x<grid.length && y≥0 && y<grid[0].length

2. the value of the neighbor is ‘1’

grid[m][n]==’1'

--

--