200.岛屿数量—1 207.课程表—1 547.省份数量 695.岛屿的最大面积 17.15.最长单词 
var numIslands =  function  ( grid)  { let ans =  0 ; let m =  grid. length; let n =  grid[ 0 ] . length; const dirs =  [ [ 1 ,  0 ] , [ - 1 ,  0 ] , [ 0 ,  1 ] , [ 0 ,  - 1 ] , ] ; const checkList =  Array ( m) . fill ( 0 ) . map ( ( )  = >  Array ( n) . fill ( 0 ) ) ; for  ( let i =  0 ;  i <  m;  i++ )  { for  ( let j =  0 ;  j <  n;  j++ )  { if  ( grid[ i] [ j]  ===  "1"  &&  checkList[ i] [ j]  ===  0 )  { ans + =  bfs ( i,  j) ; } } } function  bfs ( i,  j)  { let count =  1 ; const queue =  [ ] ; queue. push ( [ i,  j] ) ; while  ( queue. length)  { const [ x,  y]  =  queue. pop ( ) ; for  ( let [ dx,  dy]  of dirs)  { let nextX =  dx +  x; let nextY =  dy +  y; if  ( nextX >=  0  && nextX <  m && nextY >=  0  && nextY <  n && grid[ nextX] [ nextY]  ===  "1"  && checkList[ nextX] [ nextY]  ===  0 )  { queue. push ( [ nextX,  nextY] ) ; checkList[ nextX] [ nextY]  =  1 ; } } } return  count; } return  ans; 
} ; 
numIslands ( [ [ "1" ,  "1" ,  "1" ,  "1" ,  "0" ] , [ "1" ,  "1" ,  "0" ,  "1" ,  "0" ] , [ "1" ,  "1" ,  "0" ,  "0" ,  "0" ] , [ "0" ,  "0" ,  "0" ,  "0" ,  "0" ] , 
] ) ; 
var canFinish =  function  ( numCourses,  prerequisites)  { const map =  { } ; const nextMap =  { } ; for  ( let [ a,  b]  of prerequisites)  { if  ( map[ a]  ===  undefined)  { map[ a]  =  0 ; } if  ( nextMap[ b]  ===  undefined)  { nextMap[ b]  =  [ ] ; } map[ a]  + =  1 ; nextMap[ b] . push ( a) ; if  ( map[ b]  ===  undefined)  map[ b]  =  0 ; } const queue =  [ ] ; for  ( let ch in  map)  { if  ( map[ ch]  ===  0 )  { queue. push ( ch) ; } } while  ( queue. length)  { const cur =  queue. shift ( ) ; const nextCur =  nextMap[ cur] ; if  ( ! nextCur)  continue ; for  ( let ch of nextCur)  { map[ ch]  - =  1 ; if  ( map[ ch]  ===  0 )  { queue. push ( ch) ; } } } return  Object. values ( map) . every ( ( item)  = >  item ===  0 ) ; 
} ; 
canFinish ( 2 ,  [ [ 1 ,  0 ] , [ 0 ,  1 ] , 
] ) ; 
var findCircleNum =  function  ( isConnected)  { const n =  isConnected. length; const checklist =  Array ( n) . fill ( 0 ) ; let ans =  0 ; for  ( let i =  0 ;  i <  n;  i++ )  { if  ( checklist[ i]  ===  0 )  { dfs ( i) ; ans + =  1 ; } } return  ans; function  dfs ( idx)  { checklist[ idx]  =  1 ; for  ( let j =  0 ;  j <  n;  j++ )  { if  ( j !==  idx &&  checklist[ j]  ===  0  &&  isConnected[ idx] [ j]  ===  1 )  { dfs ( j) ; } } } 
} ; 
findCircleNum ( [ [ 1 ,  0 ,  0 ] , [ 0 ,  1 ,  0 ] , [ 0 ,  0 ,  1 ] , 
] ) ; 
var maxAreaOfIsland =  function  ( grid)  { const m =  grid. length; const n =  grid[ 0 ] . length; let ans =  0 ; let dirs =  [ [ - 1 ,  0 ] , [ 1 ,  0 ] , [ 0 ,  1 ] , [ 0 ,  - 1 ] , ] ; const checklist =  Array ( m) . fill ( 0 ) . map ( ( )  = >  Array ( n) . fill ( 0 ) ) ; for  ( let i =  0 ;  i <  m;  i++ )  { for  ( let j =  0 ;  j <  n;  j++ )  { if  ( checklist[ i] [ j]  ===  0  &&  grid[ i] [ j]  ===  1 )  { ans =  Math. max ( ans,  dfs ( i,  j) ) ; } } } return  ans; function  dfs ( x,  y)  { if  ( x <  0  || x >=  m || y <  0  || y >=  n || grid[ x] [ y]  ===  0  || checklist[ x] [ y]  ===  1 )  { return  0 ; } let ans =  1 ; checklist[ x] [ y]  =  1 ; for  ( let [ dx,  dy]  of dirs)  { ans + =  dfs ( dx +  x,  dy +  y) ; } return  ans; } 
} ; 
maxAreaOfIsland ( [ [ 0 ,  0 ,  1 ,  0 ,  0 ,  0 ,  0 ,  1 ,  0 ,  0 ,  0 ,  0 ,  0 ] , [ 0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  1 ,  1 ,  1 ,  0 ,  0 ,  0 ] , [ 0 ,  1 ,  1 ,  0 ,  1 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ] , [ 0 ,  1 ,  0 ,  0 ,  1 ,  1 ,  0 ,  0 ,  1 ,  0 ,  1 ,  0 ,  0 ] , [ 0 ,  1 ,  0 ,  0 ,  1 ,  1 ,  0 ,  0 ,  1 ,  1 ,  1 ,  0 ,  0 ] , [ 0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  1 ,  0 ,  0 ] , [ 0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  1 ,  1 ,  1 ,  0 ,  0 ,  0 ] , [ 0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  1 ,  1 ,  0 ,  0 ,  0 ,  0 ] , 
] ) ; 
var  longestWord  =  function  ( words )  { words. sort ( ( a,  b )  => a. length !==  b. length ?  b. length -  a. length :  a >  b ?  1  :  - 1 ) ; for  ( let  word of  words)  { if  ( isValid ( word,  0 ) )  { return  word; } } return  "" ; function  isValid ( word,  idx )  { if  ( idx >=  word. length)  return  true ; for  ( let  i =  0 ;  i <  words. length;  i++ )  { let  temp =  words[ i] ; if  ( word ===  temp)  continue ; if  ( word. substring ( idx) . indexOf ( temp)  ===  0 )  { let  res =  isValid ( word,  idx +  temp. length) ; if  ( res)  return  true ; } } return  false ; } 
} ; 
console. log ( longestWord ( [ "cat" ,  "banana" ,  "dog" ,  "nana" ,  "walk" ,  "walker" ,  "dogwalker" ] ) 
) ;