File tree Expand file tree Collapse file tree 5 files changed +136
-0
lines changed
construct-binary-tree-from-preorder-and-inorder-traversal
encode-and-decode-strings Expand file tree Collapse file tree 5 files changed +136
-0
lines changed Original file line number Diff line number Diff line change 1+ # Definition for a binary tree node.
2+ from typing import List
3+
4+
5+ class TreeNode :
6+ def __init__ (self , val = 0 , left = None , right = None ):
7+ self .val = val
8+ self .left = left
9+ self .right = right
10+
11+ class Solution :
12+ # time complexity: O(n)
13+ # space complexity: O(n)
14+ def buildTree (self , preorder : List [int ], inorder : List [int ]) -> Optional [TreeNode ]:
15+ if not preorder or not inorder :
16+ return None
17+
18+ val = preorder .pop (0 )
19+ mid = inorder .index (val )
20+ left = self .buildTree (preorder , inorder [:mid ])
21+ right = self .buildTree (preorder , inorder [mid + 1 :])
22+
23+ return TreeNode (val , left , right )
Original file line number Diff line number Diff line change 1+ class Solution1 :
2+ # time complexity: O(n)
3+ # space complexity: O(1)
4+ def countBits (self , n : int ) -> List [int ]:
5+ list = [i for i in range (n + 1 )]
6+ result = [bin (num ).count ('1' ) for num in list ]
7+ return result
8+
9+ class Solution2 :
10+ # time complexity: O(n * logn)
11+ # space complexity: O(1)
12+ def countBits (self , n : int ) -> List [int ]:
13+
14+ def count (num ):
15+ cnt = 0
16+ while num :
17+ cnt += num % 2
18+ num //= 2
19+ return cnt
20+
21+ res = [count (i ) for i in range (n + 1 )]
22+ return res
23+
24+ class Solution3 :
25+ # time complexity: O(n)
26+ # space complexity: O(1)
27+ def countBits (self , n : int ) -> List [int ]:
28+ res = [0 ] * (n + 1 )
29+ msb = 1
30+ for i in range (1 , n + 1 ):
31+ if i == msb * 2 :
32+ msb *= 2
33+ res [i ] = res [i - msb ] + 1
34+ return res
Original file line number Diff line number Diff line change 1+ class Solution :
2+ # time complexity: O(n)
3+ # space complexity: O(n)
4+ def numDecodings (self , s : str ) -> int :
5+ if not s or s .startswith ('0' ):
6+ return 0
7+
8+ n = len (s )
9+ dp = [0 ] * (n + 1 )
10+ dp [0 ] = 1
11+ dp [1 ] = 1 if s [0 ] != '0' else 0
12+
13+ for i in range (2 , n + 1 ):
14+ if s [i - 1 ] != '0' :
15+ dp [i ] += dp [i - 1 ]
16+
17+ two_digit = int (s [i - 2 :i ])
18+ if 10 <= two_digit <= 26 :
19+ dp [i ] += dp [i - 2 ]
20+
21+ return dp [n ]
Original file line number Diff line number Diff line change 1+ class Solution1 :
2+ # time complexity: O(n)
3+ # space complexity: O(1)
4+ def encode (self , strs ):
5+ return ":;" .join (strs )
6+
7+ # time complexity: O(n)
8+ # space complexity: O(1)
9+ def decode (self , str ):
10+ return str .split (":;" )
11+
12+ class Solution2 :
13+ # time complexity: O(n)
14+ # space complexity: O(1)
15+ def encode (self , strs ):
16+ txt = ""
17+ for s in strs :
18+ txt += str (len (s )) + ":" + s
19+ return txt
20+
21+ # time complexity: O(n)
22+ # space complexity: O(1)
23+ def decode (self , str ):
24+ res = []
25+ i = 0
26+ while i < len (str ):
27+ colon = str .find (":" , i )
28+ length = int (str [i :colon ])
29+ res .append (str [colon + 1 :colon + 1 + length ])
30+ i = colon + 1 + length
31+ return res
Original file line number Diff line number Diff line change 1+ from collections import defaultdict
2+
3+
4+ class Solution :
5+ # Time complexity: O(n)
6+ # Space complexity: O(n)
7+ def isAnagram (self , s : str , t : str ) -> bool :
8+ char_map = defaultdict (int )
9+ for s1 in s :
10+ char_map [s1 ] += 1
11+
12+ contrast_map = defaultdict (int )
13+ for t1 in t :
14+ contrast_map [t1 ] += 1
15+
16+ for key , val in char_map .items ():
17+ contrast_val = contrast_map [key ]
18+ if contrast_val != val :
19+ return False
20+
21+ for key , val in contrast_map .items ():
22+ char_val = char_map [key ]
23+ if char_val != val :
24+ return False
25+
26+ return True
27+
You can’t perform that action at this time.
0 commit comments