Skip to content

Commit 67e7d6b

Browse files
Create 0463-island-perimeter.js
1 parent 2d0e0cc commit 67e7d6b

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

javascript/0463-island-perimeter.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var islandPerimeter = function(grid) {
2+
const visit = new Set();
3+
4+
const dfs = function(i, j) {
5+
if(i >= grid.length || j >= grid[0].length || i < 0 || j < 0 || grid[i][j] == 0)
6+
return 1;
7+
let flatCoord = i*grid[0].length + j;
8+
if(visit.has(flatCoord))
9+
return 0;
10+
11+
visit.add(flatCoord);
12+
let perim = dfs(i, j + 1);
13+
perim += dfs(i + 1, j);
14+
perim += dfs(i, j - 1);
15+
perim += dfs(i - 1, j);
16+
return perim;
17+
};
18+
19+
for(let i = 0; i < grid.length; i++)
20+
for(let j = 0; j < grid[0].length; j++)
21+
if(grid[i][j])
22+
return dfs(i, j);
23+
};

0 commit comments

Comments
 (0)