@@ -35,6 +35,46 @@ Note: The length of each dimension in the given grid does not exceed 50.
35
35
36
36
*/
37
37
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
+
38
78
/**
39
79
* @param {number[][] } grid
40
80
* @return {number }
0 commit comments