当前位置:网站首页>[JS solution] leedcode 200 Number of islands
[JS solution] leedcode 200 Number of islands
2022-06-13 04:39:00 【xiaozhangtxue】
Subject requirements :
Here you are ‘1’( land ) and ‘0’( water ) A two-dimensional mesh of , Please calculate the number of islands in the grid .
Islands are always surrounded by water , And each island can only be combined by horizontal direction / Or a vertical connection between adjacent lands .
Besides , You can assume that all four sides of the mesh are surrounded by water .
Example 1:
Input :grid = [
[“1”,“1”,“1”,“1”,“0”],
[“1”,“1”,“0”,“1”,“0”],
[“1”,“1”,“0”,“0”,“0”],
[“0”,“0”,“0”,“0”,“0”]
]
Output :1
Example 2:
Input :grid = [
[“1”,“1”,“0”,“0”,“0”],
[“1”,“1”,“0”,“0”,“0”],
[“0”,“0”,“1”,“0”,“0”],
[“0”,“0”,“0”,“1”,“1”]
]
Output :3
Solutions : Depth traversal
JS The code is as follows :
var numIslands = function (grid) {
let res = 0
let [m, n] = [grid.length, grid[0].length]
if (m === 0 || n === 0) return 0
// Cyclic ergodic matrix
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (grid[i][j] === '1') {
res++
dfs(grid, i, j, m, n)
}
}
}
return res
}
// Depth traversal dfs
function dfs(grid, i, j, m, n) {
//console.log(grid)
if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == '0') return
grid[i][j] = '0'
dfs(grid, i - 1, j, m, n)
dfs(grid, i + 1, j, m, n)
dfs(grid, i, j - 1, m, n)
dfs(grid, i, j + 1, m, n)
}
Running results :

边栏推荐
- Uni app dynamic add style dynamic bind background image invalid
- Ladder race
- The data obtained from mongodb query data command is null
- 记录一次排查问题的经过——视频通话无法接起
- Tita: Xinrui group uses one-to-one talk to promote the success of performance change
- Converting MySQL data to PostgreSQL with Navicat
- MySQL index
- Test question bank and online simulation test for special operation certificate of construction scaffolder (special type of construction work) in 2022
- Serial communication learning
- February 25, 2021 (Archaeology 12 year Landbridge provincial competition)
猜你喜欢

【JS解决】leedcode 117. 填充每个节点的下一个右侧节点指针 II

用navicat将mysql数据转化为postgresql

2022 ICLR | CONTRASTIVE LEARNING OF IMAGE- AND STRUCTURE BASED REPRESENTATIONS IN DRUG DISCOVERY

一致性哈希的简单认识

How to use redis

一款开源的Markdown转富文本编辑器的实现原理剖析

力扣刷题647.回文子串

Mongodb compass connects to the Alibaba cloud remote server database or reports an error occurred while loading instance info: command hostinfo req

Express scaffold creation

PowerShell plus domain add computer module
随机推荐
工业互联网通用通信协议
Nodejs parsing get request URL string
力扣刷题647.回文子串
Redis主从复制、哨兵模式、集群
Mysql database installation
Powershell 加域 Add-Computer模块
Catalan number
PowerDesigner easy to use
[flutter problem Series Chapter 67] the Solution to the problem of Routing cannot be jumped again in in dialog popup Using get plug - in in flutter
Applet waterfall flow
PHP security development 15 user password modification module
JS, how to add grid style
Applet - uniapp realizes the functions of two-dimensional code picture pop-up and picture saving
剑指 Offer 56 - I. 数组中数字出现的次数
PowerShell: because running scripts is prohibited on this system, the solution
2022 ICML | Pocket2Mol: Efficient Molecular Sampling Based on 3D Protein Pockets
正态分布(高斯分布)
Use go to add massive data to MySQL
Tita:新锐集团采用一对一面谈推动绩效变革成功
Solve the problem of running server nodemon reporting errors