Skip to content
New issue

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

岛屿系列问题 #160

Open
TieMuZhen opened this issue Apr 8, 2022 · 0 comments
Open

岛屿系列问题 #160

TieMuZhen opened this issue Apr 8, 2022 · 0 comments
Labels

Comments

@TieMuZhen
Copy link
Owner

一、岛屿数量(LeetCode 200)

image

/**
 * @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;
};

二、岛屿的周长(LeetCode 463)

image

/**
 * @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;
};

三、岛屿的最大面积(LeetCode 695)

image
image
image
image
image

/**
 * @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;
};

四、不同岛屿的数量(LeetCode 694)

image

/**
 * @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;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant