File tree Expand file tree Collapse file tree 5 files changed +130
-0
lines changed
construct-binary-tree-from-preorder-and-inorder-traversal
longest-palindromic-substring Expand file tree Collapse file tree 5 files changed +130
-0
lines changed Original file line number Diff line number Diff line change 1+ from typing import (
2+ List ,
3+ )
4+
5+ import heapq
6+ heap = [c for c in indegree if indegree [c ] == 0 ]
7+ heapq .heapify (heap )
8+ res = []
9+
10+
11+ class Solution :
12+ """
13+ @param words: a list of words
14+ @return: a string which is correct order
15+ """
16+ def alien_order (self , words : List [str ]) -> str :
17+ # Write your code here
18+ adj = {c : set () for word in words for c in word }
19+ indegree = {c : 0 for c in adj }
20+
21+ for i in range (len (words ) - 1 ):
22+ w1 , w2 = words [i ], words [i + 1 ]
23+ minlen = min (len (w1 ), len (w2 ))
24+ if len (w1 ) > len (w2 ) and w1 [:minlen ] == w2 [:minlen ]:
25+ return ""
26+ for j in range (minlen ):
27+ if w1 [j ] != w2 [j ]:
28+ if w2 [j ] not in adj [w1 [j ]]:
29+ adj [w1 [j ]].add (w2 [j ])
30+ indegree [w2 [j ]] += 1
31+ break
32+
33+ while heap :
34+ c = heapq .heappop (heap )
35+ res .append (c )
36+ for nei in adj [c ]:
37+ indegree [nei ] -= 1
38+ if indegree [nei ] == 0 :
39+ heapq .heappush (heap , nei )
40+
41+ if len (res ) != len (adj ):
42+ return ""
43+
44+ return "" .join (res )
Original file line number Diff line number Diff line change 1+ # Definition for a binary tree node.
2+ # class TreeNode:
3+ # def __init__(self, val=0, left=None, right=None):
4+ # self.val = val
5+ # self.left = left
6+ # self.right = right
7+ class Solution :
8+ def buildTree (self , preorder : List [int ], inorder : List [int ]) -> Optional [TreeNode ]:
9+ if not inorder :
10+ return None
11+
12+ val = preorder .pop (0 )
13+ mid = inorder .index (val )
14+ left = self .buildTree (preorder , inorder [:mid ])
15+ right = self .buildTree (preorder , inorder [mid + 1 :])
16+ return TreeNode (val , left , right )
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def longestPalindrome (self , s : str ) -> str :
3+ # if len(s) <= 2:
4+ # return s[0]
5+
6+ # for i in range(2, len(s)):
7+ # for k in range(len(s)-i):
8+ # if s[k:k+i] == s[k:k+i][::-1]:
9+ # return s[k:k+i]
10+
11+ max_s , max_e = 0 , 0
12+
13+ for i in range (len (s )):
14+ start , end = i , i
15+ while 0 <= start and end < len (s ) and s [start ] == s [end ]:
16+ if max_e - max_s < end - start :
17+ max_s , max_e = start , end
18+ start , end = start - 1 , end + 1
19+
20+ start , end = i , i + 1
21+ while 0 <= start and end < len (s ) and s [start ] == s [end ]:
22+ if max_e - max_s < end - start :
23+ max_s , max_e = start , end
24+ start , end = start - 1 , end + 1
25+
26+ return s [max_s : max_e + 1 ]
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def rotate (self , matrix : List [List [int ]]) -> None :
3+ """
4+ Do not return anything, modify matrix in-place instead.
5+ [0][0] [0][1] [0][2]
6+ [1][0] [1][1] [1][2]
7+ [2][0] [2][1] [2][2]
8+
9+ [2][0] [1][0] [0][0]
10+ [2][1] [1][1] [0][1]
11+ [2][2] [1][2] [0][2]
12+ """
13+ n = len (matrix )
14+
15+ for i in range (n ):
16+ for j in range (i , n ):
17+ matrix [i ][j ], matrix [j ][i ] = matrix [j ][i ], matrix [i ][j ]
18+
19+ for i in range (n ):
20+ matrix [i ].reverse ()
Original file line number Diff line number Diff line change 1+ # Definition for a binary tree node.
2+ # class TreeNode:
3+ # def __init__(self, val=0, left=None, right=None):
4+ # self.val = val
5+ # self.left = left
6+ # self.right = right
7+ class Solution :
8+ def isSubtree (self , root : Optional [TreeNode ], subRoot : Optional [TreeNode ]) -> bool :
9+ if not subRoot :
10+ return True
11+ if not root :
12+ return False
13+
14+ def same (root , subRoot ):
15+ if not root or not subRoot :
16+ return not root and not subRoot
17+
18+ if root .val != subRoot .val :
19+ return False
20+ return same (root .left , subRoot .left ) and same (root .right , subRoot .right )
21+
22+ if same (root , subRoot ):
23+ return True
24+ return self .isSubtree (root .left , subRoot ) or self .isSubtree (root .right , subRoot )
You can’t perform that action at this time.
0 commit comments