We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
原题链接
先明确,题意要求我们找到矩阵中的岛屿数量,上下左右相连接的 '1' 是连续的 1 座岛屿。
grid[i][j] === '1'
const numIslands = function(grid) { const dfs = function(grid, i, j) { if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] === '0') { return } grid[i][j] = '0' dfs(grid, i + 1, j) dfs(grid, i, j + 1) dfs(grid, i - 1, j) dfs(grid, i, j - 1) } let count = 0 for (let i = 0; i < grid.length; i++) { for (let j = 0; j < grid[0].length; j++) { if (grid[i][j] === '1') { dfs(grid, i, j) count++ } } } return count }
The text was updated successfully, but these errors were encountered:
666
Sorry, something went wrong.
No branches or pull requests
原题链接
深度优先遍历
先明确,题意要求我们找到矩阵中的岛屿数量,上下左右相连接的 '1' 是连续的 1 座岛屿。
grid[i][j] === '1'
时,进行搜索并且将岛屿数加 1。The text was updated successfully, but these errors were encountered: