File tree Expand file tree Collapse file tree 5 files changed +191
-0
lines changed
construct-binary-tree-from-preorder-and-inorder-traversal
longest-palindromic-substring Expand file tree Collapse file tree 5 files changed +191
-0
lines changed Original file line number Diff line number Diff line change 1+ export class Solution {
2+ /**
3+ * @param {string[] } words
4+ * @return {string }
5+ */
6+ alienOrder ( words ) {
7+ const adj = new Map ( ) ;
8+ for ( const word of words ) {
9+ for ( const char of word ) {
10+ if ( ! adj . has ( char ) ) {
11+ adj . set ( char , new Set ( ) ) ;
12+ }
13+ }
14+ }
15+
16+ for ( let i = 0 ; i < words . length - 1 ; i ++ ) {
17+ const w1 = words [ i ] ;
18+ const w2 = words [ i + 1 ] ;
19+ const minLen = Math . min ( w1 . length , w2 . length ) ;
20+
21+ if ( w1 . length > w2 . length && w1 . slice ( 0 , minLen ) === w2 . slice ( 0 , minLen ) ) {
22+ return "" ;
23+ }
24+
25+ for ( let j = 0 ; j < minLen ; j ++ ) {
26+ if ( w1 [ j ] !== w2 [ j ] ) {
27+ adj . get ( w1 [ j ] ) . add ( w2 [ j ] ) ;
28+ break ;
29+ }
30+ }
31+ }
32+
33+ const visit = { } ; // false: visited, true: current path (cycle detection)
34+ const res = [ ] ;
35+
36+ const dfs = ( c ) => {
37+ if ( c in visit ) return visit [ c ] ;
38+
39+ visit [ c ] = true ;
40+ for ( const nei of adj . get ( c ) ) {
41+ if ( dfs ( nei ) ) return true ;
42+ }
43+ visit [ c ] = false ;
44+ res . push ( c ) ;
45+ return false ;
46+ } ;
47+
48+ for ( const c of adj . keys ( ) ) {
49+ if ( dfs ( c ) ) return "" ;
50+ }
51+
52+ return res . reverse ( ) . join ( '' ) ;
53+ }
54+ }
55+
Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for a binary tree node.
3+ * function TreeNode(val, left, right) {
4+ * this.val = (val===undefined ? 0 : val)
5+ * this.left = (left===undefined ? null : left)
6+ * this.right = (right===undefined ? null : right)
7+ * }
8+ */
9+
10+ /**
11+ * @param {number[] } preorder
12+ * @param {number[] } inorder
13+ * @return {TreeNode }
14+ */
15+ var buildTree = function ( preorder , inorder ) {
16+ if ( ! preorder . length || ! inorder . length ) {
17+ return null ;
18+ }
19+
20+ const root = new TreeNode ( preorder [ 0 ] ) ;
21+ const mid = inorder . indexOf ( preorder [ 0 ] ) ;
22+
23+ root . left = buildTree ( preorder . slice ( 1 , mid + 1 ) , inorder . slice ( 0 , mid ) ) ;
24+ root . right = buildTree ( preorder . slice ( mid + 1 ) , inorder . slice ( mid + 1 ) ) ;
25+
26+ return root ;
27+ } ;
28+
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string } s
3+ * @return {string }
4+ */
5+ var longestPalindrome = function ( s ) {
6+ let res = "" ;
7+ let resLen = 0 ;
8+
9+ for ( let i = 0 ; i < s . length ; i ++ ) {
10+ // Odd length
11+ let l = i ,
12+ r = i ;
13+ while ( l >= 0 && r < s . length && s [ l ] === s [ r ] ) {
14+ if ( r - l + 1 > resLen ) {
15+ res = s . slice ( l , r + 1 ) ;
16+ resLen = r - l + 1 ;
17+ }
18+ l -- ;
19+ r ++ ;
20+ }
21+
22+ // Even length
23+ l = i , r = i + 1 ;
24+ while ( l >= 0 && r < s . length && s [ l ] === s [ r ] ) {
25+ if ( r - l + 1 > resLen ) {
26+ res = s . slice ( l , r + 1 ) ;
27+ resLen = r - l + 1 ;
28+ }
29+ l -- ;
30+ r ++ ;
31+ }
32+ }
33+
34+ return res ;
35+ } ;
36+
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[][] } matrix
3+ * @return {void } Do not return anything, modify matrix in-place instead.
4+ */
5+ var rotate = function ( matrix ) {
6+ let l = 0 ;
7+ let r = matrix . length - 1 ;
8+
9+ while ( l < r ) {
10+ for ( let i = 0 ; i < r - l ; i ++ ) {
11+ let top = l ;
12+ let bottom = r ;
13+
14+ // save the top-left
15+ let topLeft = matrix [ top ] [ l + i ] ;
16+
17+ // move bottom-left into top-left
18+ matrix [ top ] [ l + i ] = matrix [ bottom - i ] [ l ] ;
19+
20+ // move bottom-right into bottom-left
21+ matrix [ bottom - i ] [ l ] = matrix [ bottom ] [ r - i ] ;
22+
23+ // move top-right into bottom-right
24+ matrix [ bottom ] [ r - i ] = matrix [ top + i ] [ r ] ;
25+
26+ // move top-left into top-right
27+ matrix [ top + i ] [ r ] = topLeft ;
28+ }
29+ r -- ;
30+ l ++ ;
31+ }
32+ } ;
33+
Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for a binary tree node.
3+ * function TreeNode(val, left, right) {
4+ * this.val = (val===undefined ? 0 : val)
5+ * this.left = (left===undefined ? null : left)
6+ * this.right = (right===undefined ? null : right)
7+ * }
8+ */
9+
10+ /**
11+ * @param {TreeNode } s
12+ * @param {TreeNode } t
13+ * @return {boolean }
14+ */
15+ var isSubtree = function ( s , t ) {
16+ if ( ! t ) return true ;
17+ if ( ! s ) return false ;
18+
19+ if ( sameTree ( s , t ) ) {
20+ return true ;
21+ }
22+ return isSubtree ( s . left , t ) || isSubtree ( s . right , t ) ;
23+ } ;
24+
25+ /**
26+ * @param {TreeNode } s
27+ * @param {TreeNode } t
28+ * @return {boolean }
29+ */
30+ var sameTree = function ( s , t ) {
31+ if ( ! s && ! t ) return true ;
32+
33+ if ( s && t && s . val === t . val ) {
34+ return sameTree ( s . left , t . left ) && sameTree ( s . right , t . right ) ;
35+ }
36+
37+ return false ;
38+ } ;
39+
You can’t perform that action at this time.
0 commit comments