成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

[Leetcode] Number of Islands 島嶼數(shù)量(JavaScript 實現(xiàn))

pingan8787 / 1797人閱讀

摘要:解題思路標(biāo)零法對這個矩陣進(jìn)行循環(huán)訪問每一個點當(dāng)這個點等于,島嶼數(shù)量,與其同時用算法訪問周圍的其他點,進(jìn)行同樣的操作且將訪問過且等于的點標(biāo)記為零。版本島嶼數(shù)量搜索右邊搜索左邊搜索下邊搜索上邊

Q: Number of Islands

Given a 2d grid map of "1"s (land) and "0"s (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

Example 1:

Input:
11110
11010
11000
00000

Output: 1

Example 2:

Input:
11000
11000
00100
00011

Output: 3

解題思路

標(biāo)零法

對這個矩陣進(jìn)行循環(huán)訪問每一個點;
當(dāng)這個點等于 1,島嶼數(shù)量 count++,與其同時用 dfs 算法(depth first search)訪問周圍的其他點,進(jìn)行同樣的操作;
且將訪問過且等于 1 的點標(biāo)記為零。

Solutions (JavaScript 版本)
/**
 * @param {character[][]} grid
 * @return {number}
 */
var numIslands = function(grid) {
    var count = 0    // 島嶼數(shù)量
    if (grid.length === 0) return count
    for (var i = 0; i < grid.length; i++) {
        for(var j = 0; j < grid[0].length; j++) {
            if (grid[i][j] === "1") {
                dfsSearch(grid, i, j)
                count++
            }
        }
    }
    return count
};

function dfsSearch(grid, i, j) {
    if(i < 0 || j < 0 || i >= grid.length || j >= grid[0].length) return;
    if(grid[i][j] === "1"){
        grid[i][j] = "0";
        dfsSearch(grid, i + 1, j);    // 搜索右邊
        dfsSearch(grid, i - 1, j);    // 搜索左邊
        dfsSearch(grid, i, j + 1);    // 搜索下邊
        dfsSearch(grid, i, j - 1);    // 搜索上邊
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/98029.html

相關(guān)文章

  • [Leetcode] Number of Islands 島嶼個數(shù)

    摘要:同時我們每找到一個,就將其標(biāo)為,這樣就能把整個島嶼變成。我們只要記錄對矩陣遍歷時能進(jìn)入多少次搜索,就代表有多少個島嶼。 Number of Islands 最新更新的思路,以及題II的解法請訪問:https://yanjia.me/zh/2018/11/... Given a 2d grid map of 1s (land) and 0s (water), count the nu...

    Raaabbit 評論0 收藏0
  • [LeetCode/LintCode] Number of Islands [DFS]

    摘要:兩個循環(huán)遍歷整個矩陣,出現(xiàn)則將其周圍相鄰的全部標(biāo)記為,用子函數(shù)遞歸標(biāo)記。注意里每次遞歸都要判斷邊界。寫一個的,寫熟練。 Number of Islands Problem Given a boolean/char 2D matrix, find the number of islands. 0 is represented as the sea, 1 is represented as...

    Fourierr 評論0 收藏0
  • [LeetCode] 694. Number of Distinct Islands

    Problem Given a non-empty 2D array grid of 0s and 1s, an island is a group of 1s (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are s...

    SunZhaopeng 評論0 收藏0
  • leetcode200. Number of Islands

    摘要:題目要求提供一個二維數(shù)組表示一張地圖,其中代表陸地,代表海洋。這里使用一個新的二維數(shù)組來表示對應(yīng)地圖上的元素屬于哪個并查集。在合并的時候先進(jìn)行判斷,如果二者為已經(jīng)相連的陸地,則無需合并,否則將新的二維數(shù)組上的元素指向所在的并查集。 題目要求 Given a 2d grid map of 1s (land) and 0s (water), count the number of isla...

    Zoom 評論0 收藏0
  • 前端 | 每天一個 LeetCode

    摘要:在線網(wǎng)站地址我的微信公眾號完整題目列表從年月日起,每天更新一題,順序從易到難,目前已更新個題。這是項目地址歡迎一起交流學(xué)習(xí)。 這篇文章記錄我練習(xí)的 LeetCode 題目,語言 JavaScript。 在線網(wǎng)站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公眾號: showImg(htt...

    張漢慶 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<