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

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'

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store