1. const dfs = function (grid, x, y, col, row) {
    2. if (x < 0 || x >= col || y < 0 || y >= row || grid[x][y] === '0') {
    3. return
    4. }
    5. grid[x][y] = '0'
    6. dfs(grid, x + 1, y, col, row)
    7. dfs(grid, x - 1, y, col, row)
    8. dfs(grid, x, y + 1, col, row)
    9. dfs(grid, x, y - 1, col, row)
    10. }
    11. /**
    12. * @param {character[][]} grid
    13. * @return {number}
    14. */
    15. var numIslands = function (grid) {
    16. if (!grid || !grid.length) return 0
    17. const row = grid.length,
    18. col = grid[0].length
    19. let result = 0
    20. for (let i = 0; i < row; i++) {
    21. for (let j = 0; j < col; j++) {
    22. if (grid[i][j] === '1') {
    23. result += 1
    24. dfs(grid, i, j, row, col)
    25. }
    26. }
    27. }
    28. return result
    29. };
    1. var numIslands = function (grid) {
    2. if (!grid || !grid.length) return 0
    3. const row = grid.length,
    4. col = grid[0].length,
    5. queue = []
    6. let result = 0
    7. for (let i = 0; i < row; i++) {
    8. for (let j = 0; j < col; j++) {
    9. if (grid[i][j] === '1') {
    10. result += 1
    11. queue.push([i, j])
    12. grid[i][j] = '0'
    13. while (queue.length) {
    14. const [x, y] = queue.shift()
    15. if (x - 1 >= 0 && grid[x - 1][y] === '1') {
    16. grid[x - 1][y] = '0'
    17. queue.push([x - 1, y])
    18. }
    19. if (x + 1 < row && grid[x + 1][y] === '1') {
    20. grid[x + 1][y] = '0'
    21. queue.push([x + 1, y])
    22. }
    23. if (y - 1 >= 0 && grid[x][y - 1] === '1') {
    24. grid[x][y - 1] = '0'
    25. queue.push([x, y - 1])
    26. }
    27. if (y + 1 < col && grid[x][y + 1] === '1') {
    28. grid[x][y + 1] = '0'
    29. queue.push([x, y + 1])
    30. }
    31. }
    32. }
    33. }
    34. }
    35. return result
    36. };
    1. var numIslands = function (grid) {
    2. if (!grid || !grid.length) return 0
    3. const row = grid.length,
    4. col = grid[0].length
    5. let water = 0
    6. const uf = new UnionFind(grid)
    7. for (let i = 0; i < row; i++) {
    8. for (let j = 0; j < col; j++) {
    9. if(grid[i][j] === '0') {
    10. water++
    11. } else {
    12. const directions = [[0,1], [0, -1], [1, 0], [-1, 0]]
    13. for(let k of directions) {
    14. const x = i + k[0]
    15. const y = j + k[1]
    16. if(x >=0 && y >= 0 && x < row && y < col && grid[x][y] === '1') {
    17. uf.union(x * col + y, i * col + j)
    18. }
    19. }
    20. }
    21. }
    22. }
    23. return uf.getCount() - water
    24. };
    25. class UnionFind {
    26. root = null
    27. count = 0
    28. constructor(grid) {
    29. const row = grid.length
    30. const col = grid[0].length
    31. this.count = row * col
    32. this.root = new Array(this.count)
    33. for(let i = 0; i < this.count; i++) {
    34. this.root[i] = i
    35. }
    36. }
    37. find(x) {
    38. if(x === this.root[x]) return x
    39. return this.root[x] = this.find(this.root[x])
    40. }
    41. union(x, y) {
    42. const rootX = this.find(x)
    43. const rootY = this.find(y)
    44. if(rootX !== rootY) {
    45. this.root[rootX] = rootY
    46. this.count--
    47. }
    48. }
    49. getCount() {
    50. return this.count
    51. }
    52. }