-
Notifications
You must be signed in to change notification settings - Fork 279
/
200 Number of Islands.js
94 lines (77 loc) · 2.14 KB
/
200 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// 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:
// 11110
// 11010
// 11000
// 00000
// Answer: 1
// Example 2:
// 11000
// 11000
// 00100
// 00011
// Answer: 3
/**
* @param {character[][]} grid
* @return {number}
*/
var numIslands = function(grid) {
var visited = [];
var row = grid.length;
if(!grid || row === 0) {
return 0;
}
var col = grid[0].length;
var count = 0;
for(var i = 0; i < row; i++) {
for(var j = 0; j < col; j++) {
if(grid[i][j] === '1') {
count++;
traverse(i, j, grid, row, col);
}
}
}
return count;
};
var traverse = function(i, j, grid, row, col) {
if((0 <= i && i < row) && (0 <= j && j < col) && grid[i][j] === '1') {
grid[i][j] = '2';
traverse(i + 1, j, grid, row, col);
traverse(i, j + 1, grid, row, col);
traverse(i - 1, j, grid, row, col);
traverse(i, j - 1, grid, row, col);
}
}
// non recusion
/**
* @param {character[][]} grid
* @return {number}
*/
var numIslands = function(grid) {
var count = 0;
function traverseIsland(i, j, grid) {
var stack = [];
stack.push([i, j]);
while(stack.length) {
var pair = stack.pop();
i = pair[0];
j = pair[1];
if(i >= 0 && i < grid.length && j >= 0 && j < grid[0].length && grid[i][j] === '1') {
grid[i][j] = '2';
stack.push([i + 1, j]);
stack.push([i - 1, j]);
stack.push([i, j + 1]);
stack.push([i, j - 1]);
}
}
}
for(var i = 0; i < grid.length; i++) {
for(var j = 0; j < grid[0].length; j++) {
if(grid[i][j] === '1') {
traverseIsland(i, j, grid);
count++;
}
}
}
return count;
};