-
Notifications
You must be signed in to change notification settings - Fork 1
/
number-of-islands.js
30 lines (29 loc) · 958 Bytes
/
number-of-islands.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* @param {character[][]} grid
* @return {number}
* [["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]]
* [["1","1","1"],["0","1","0"],["1","1","1"]]
*/
var numIslands = function(grid) {
let isle = 0
for(let r = 0; r < grid.length; r++) {
for(let c = 0; c < grid[0].length; c++) {
console.log(`For position [${r}][${c}]: ${grid[r][c]}`);
if (grid[r][c] === "1" &&
grid[r][c-1] !== "1" && // left
grid[r][c+1] !== "1" && // right
(r>0 && grid[r-1][c] !== "1") && // top
grid[r+1][c] !== "1" // bottom
) {
console.log(' increase island');
isle++;
}
}
}
return isle;
};
(() => {
const grid = [["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]];
let r = numIslands(grid);
console.log(r);
})();