Skip to content

Commit 94b554c

Browse files
Added number of Islands
1 parent 5a9c2e3 commit 94b554c

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

LeetcodeProblems/Number_of_Islands.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
Number of Islands
3+
https://leetcode.com/problems/number-of-islands/
4+
5+
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.
6+
7+
Example 1:
8+
9+
Input:
10+
11110
11+
11010
12+
11000
13+
00000
14+
15+
Output: 1
16+
Example 2:
17+
18+
Input:
19+
11000
20+
11000
21+
00100
22+
00011
23+
24+
Output: 3
25+
*/
26+
27+
/*
28+
* @param {character[][]} grid
29+
* @return {number}
30+
*/
31+
var numIslands = function(grid) {
32+
if(grid.length === 0)
33+
return 0;
34+
35+
var countIslands = 0;
36+
const rowsCount = grid.length;
37+
const columnsCount = grid[0].length;
38+
for(var i = 0; i < rowsCount; i++) {
39+
for(var j = 0; j < columnsCount; j++) {
40+
if(grid[i][j] == 1) {
41+
countIslands++;
42+
colorIsland(grid, i, j, rowsCount, columnsCount);
43+
}
44+
}
45+
}
46+
47+
return countIslands;
48+
};
49+
50+
var colorIsland = function(grid, i, j, rowsCount, columnsCount) {
51+
if(i < 0 || j < 0 || i >= rowsCount || j >= columnsCount || grid[i][j] == 0)
52+
return;
53+
54+
grid[i][j] = 0;
55+
56+
colorIsland(grid, i - 1, j, rowsCount, columnsCount);
57+
colorIsland(grid, i + 1, j, rowsCount, columnsCount);
58+
colorIsland(grid, i, j - 1, rowsCount, columnsCount);
59+
colorIsland(grid, i, j + 1, rowsCount, columnsCount);
60+
}
61+
62+
var main = function() {
63+
console.log(numIslands([[1]]));
64+
console.log(numIslands([]));
65+
console.log(numIslands(
66+
[
67+
["1","1","1","1","0"],
68+
["1","1","0","1","0"],
69+
["1","1","0","0","0"],
70+
["0","0","0","0","0"]
71+
])
72+
);
73+
}
74+
75+
module.exports.main = main;

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Solutions of algorithm problems using Javascript
1414
| [merge k sorted lists ](/LeetcodeProblems/merge_k_sorted_lists.js) | Hard | https://leetcode.com/problems/merge-k-sorted-lists/ |
1515
| [Subarray Sum Equals K ](/LeetcodeProblems/Subarray_Sum_Equals_K.js) | Medium | https://leetcode.com/problems/subarray-sum-equals-k/ |
1616
| [3Sum ](/LeetcodeProblems/3Sum.js) | Medium | https://leetcode.com/problems/3sum/ |
17+
| [NumberOfIslands ](/LeetcodeProblems/Number_of_Islands.js) | Medium | https://leetcode.com/problems/number-of-islands/ |
1718
| [Swap Nodes in Pairs](/LeetcodeProblems/Swap_Nodes_in_Pairs.js) | Medium | https://leetcode.com/problems/swap-nodes-in-pairs/ |
1819
| [Add Two Numbers ](/LeetcodeProblems/Add_Two_Numbers.js) | Medium | https://leetcode.com/problems/add-two-numbers/ |
1920
| [Clone Graph ](/LeetcodeProblems/Clone_Graph.js) | Medium | https://leetcode.com/problems/clone-graph/ |

0 commit comments

Comments
 (0)