Code Monkey home page Code Monkey logo

Comments (2)

bazinga-web avatar bazinga-web commented on June 17, 2024

解题思路:

  1. 遍历二维数组,遇到“1”则让小岛数量加1
  2. 然后根据当前位置扩散,使用深度搜索的方式沉没小岛

bfs: (宽)广度优先搜索
dfs: 深度优先搜索

/**
 * @param {character[][]} grid
 * @return {number}
 */
var numIslands = function (grid) {
    let count = 0;
    function dfs(row, col) {
        if (
            row < 0 ||
            row >= grid.length ||
            col < 0 ||
            col >= grid[0].length ||
            grid[row][col] === "0"
        ) {
            return;
        }
        grid[row][col] = "0";
        dfs(row + 1, col);
        dfs(row - 1, col);
        dfs(row, col + 1);
        dfs(row, col - 1);
    }
    for (let row = 0; row < grid.length; row++) {
        for (let col = 0; col < grid[0].length; col++) {
            if (grid[row][col] === "1") {
                count++;
                dfs(row, col);
            }
        }
    }
    return count;
};

from like-algorithms.

Ray-56 avatar Ray-56 commented on June 17, 2024

深度优先遍历

  • 遇到 1 岛屿数量加一的同时调用递归边界判断
function helper(grid, x, y, rowLen, colLen) {
    if (x < 0 || y < 0 || x > rowLen - 1 || y > colLen - 1 || grid[x][y] == '0') return;

    grid[x][y] = '0';

    helper(grid, x - 1, y, rowLen, colLen);
    helper(grid, x + 1, y, rowLen, colLen);
    helper(grid, x, y - 1, rowLen, colLen);
    helper(grid, x, y + 1, rowLen, colLen);
}
/**
 * @param {character[][]} grid
 * @return {number}
 */
var numIslands = function(grid) {
    // DFS
    let isLand = 0;
    const rowLen = grid.length;
    if (rowLen === 0) return 0;
    const colLen = grid[0].length;

    for (let x = 0; x < rowLen; x++) {
        for (let y = 0; y < colLen; y++) {
            if (grid[x][y] == '1') {
                helper(grid, x, y, rowLen, colLen);
                isLand++;
            }
        }
    }

    return isLand;
};

from like-algorithms.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.