Skip to content

Commit 67949d0

Browse files
authored
Update 694-number-of-distinct-islands.js
1 parent 5ab9a34 commit 67949d0

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

694-number-of-distinct-islands.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,46 @@ Note: The length of each dimension in the given grid does not exceed 50.
3535
3636
*/
3737

38+
/**
39+
* @param {number[][]} grid
40+
* @return {number}
41+
*/
42+
const numDistinctIslands = function(grid) {
43+
const set = new Set()
44+
for (let i = 0; i < grid.length; i++) {
45+
for (let j = 0; j < grid[0].length; j++) {
46+
if (grid[i][j] === 1) {
47+
const tempArr = []
48+
helper(i, j, grid, tempArr)
49+
const x = tempArr[0][0] - 0
50+
const y = tempArr[0][1] - 0
51+
let str = ''
52+
for (let k = 0; k < tempArr.length; k++) {
53+
str += '#' + (tempArr[k][0] - x) + '#' + (tempArr[k][1] - y)
54+
}
55+
set.add(str)
56+
}
57+
}
58+
}
59+
return set.size
60+
}
61+
62+
function helper(i, j, arr, tempArr) {
63+
tempArr.push([i, j])
64+
arr[i][j] = 0
65+
66+
if (arr[i][j - 1] === 1) helper(i, j - 1, arr, tempArr)
67+
if (arr[i][j + 1] === 1) helper(i, j + 1, arr, tempArr)
68+
if (arr[i - 1]) {
69+
if (arr[i - 1][j] === 1) helper(i - 1, j, arr, tempArr)
70+
}
71+
if (arr[i + 1]) {
72+
if (arr[i + 1][j] === 1) helper(i + 1, j, arr, tempArr)
73+
}
74+
}
75+
76+
// another
77+
3878
/**
3979
* @param {number[][]} grid
4080
* @return {number}

0 commit comments

Comments
 (0)