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
/** * @param {character[][]} grid * @return {number} */ let count; function dfs(grid, row, col){ if(row < 0 || row > grid.length - 1 || col < 0 || col > grid[0].length - 1 || grid[row][col] == "0"){ return; } grid[row][col] = "0"; dfs(grid, row - 1, col); dfs(grid, row + 1, col); dfs(grid, row, col - 1); dfs(grid, row, col + 1); } var numIslands = function(grid) { count = 0; for(let i = 0; i < grid.length; i++){ for(let j = 0; j < grid[0].length; j++){ if(grid[i][j] == "1"){ count++; dfs(grid, i, j); } } } return count; };
/** * @param {number[][]} grid * @return {number} */ var dfs = function(grid, row, col){ let count = 0; // 上 if(row - 1 >= 0){ if(grid[row - 1][col] == 0 && grid[row][col] == 1) count++; }else if(row == 0){ if(grid[row][col] == 1) count++; } // 下 if(row + 1 <= grid.length - 1){ if(grid[row + 1][col] == 0 && grid[row][col] == 1) count++; }else if(row == grid.length - 1){ if(grid[row][col] == 1) count++; } // 左 if(col - 1 >= 0){ if(grid[row][col - 1] == 0 && grid[row][col] == 1) count++; }else if(col == 0){ if(grid[row][col] == 1) count++; } // 右 if(col + 1 <= grid[0].length - 1){ if(grid[row][col + 1] == 0 && grid[row][col] == 1) count++; }else if(col == grid[0].length - 1){ if(grid[row][col] == 1) count++; } return count; } var islandPerimeter = function(grid) { let sum = 0; for(let i = 0; i < grid.length; i++){ for(let j = 0; j < grid[0].length; j++){ sum += dfs(grid, i, j); } } return sum; };
/** * @param {number[][]} grid * @return {number} */ var maxAreaOfIsland = function(grid) { let maxLand = 0; function dfs(row, col){ if(row < 0 || row > grid.length - 1 || col < 0 || col > grid[0].length - 1 || grid[row][col] == 0){ return 0; }else{ grid[row][col] = 0; return 1 + dfs(row - 1, col) + dfs(row + 1, col) + dfs(row, col - 1) + dfs(row, col + 1); } } for(let i = 0; i < grid.length; i++){ for(let j = 0; j < grid[0].length; j++){ maxLand = Math.max(maxLand, dfs(i, j)); } } return maxLand; };
/** * @param {character[][]} grid * @return {number} */ function pushReturnArray(arr, data){ // arr.push()返回值为arr长度,我们这里需要arr数组 arr.push(data); return arr; } function dfs(grid, row, col, arr){ if(row < 0 || row > grid.length - 1 || col < 0 || col > grid[0].length - 1 || grid[row][col] == "0"){ return; } grid[row][col] = "0"; // 标志走过的,防止重复访问 dfs(grid, row - 1, col, pushReturnArray(arr, "up")); dfs(grid, row + 1, col, pushReturnArray(arr, "down")); dfs(grid, row, col - 1, pushReturnArray(arr, "left")); dfs(grid, row, col + 1, pushReturnArray(arr, "right")); } var numIslands = function(grid) { let set = new Set(); // 去重 for(let i = 0; i < grid.length; i++){ for(let j = 0; j < grid[0].length; j++){ if(grid[i][j] == "1"){ const arr = []; dfs(grid, i, j, arr); set.add(arr.join()); } } } return Array.from(set).length; };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
一、岛屿数量(LeetCode 200)
二、岛屿的周长(LeetCode 463)
三、岛屿的最大面积(LeetCode 695)
四、不同岛屿的数量(LeetCode 694)
The text was updated successfully, but these errors were encountered: