矩阵

岛屿数量

function numIslands(grid: string[][]): number {
    function dfs(i, j) {
        if (0 <= i && i < row && 0 <= j && j < col && grid[i][j] === '1') {
            grid[i][j] = '0'
            dfs(i - 1, j)
            dfs(i + 1, j)
            dfs(i, j - 1)
            dfs(i, j + 1)
        }
    }
 
    let [row, col] = [grid.length, grid[0].length]
    let count = 0
    for (let i = 0; i < row; i++) {
        for (let j = 0; j < col; j++) {
            if (grid[i][j] === '1') {
                dfs(i, j)
                count++
            }
        }
    }
 
    return count
};