File tree Expand file tree Collapse file tree 5 files changed +92
-0
lines changed
construct-binary-tree-from-preorder-and-inorder-traversal Expand file tree Collapse file tree 5 files changed +92
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def threeSum (self , nums : List [int ]) -> List [List [int ]]:
3
+ answerSet = set ()
4
+ nums .sort ()
5
+
6
+ for i in range (len (nums ) - 2 ):
7
+ leftIdx = i + 1
8
+ rightIdx = len (nums ) - 1
9
+ while leftIdx < rightIdx :
10
+ sum = nums [i ] + nums [leftIdx ] + nums [rightIdx ]
11
+ if sum < 0 :
12
+ leftIdx += 1
13
+ elif sum > 0 :
14
+ rightIdx -= 1
15
+ else :
16
+ answerSet .add ((nums [i ], nums [leftIdx ], nums [rightIdx ]))
17
+ leftIdx = leftIdx + 1
18
+ rightIdx = rightIdx - 1
19
+
20
+ return list (answerSet )
21
+
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def climbStairs (self , n : int ) -> int :
3
+ dp = []
4
+ dp .append (0 )
5
+ dp .append (1 )
6
+ dp .append (2 )
7
+
8
+ for i in range (3 , n + 1 ):
9
+ dp .append (dp [i - 1 ] + dp [i - 2 ])
10
+
11
+ return dp [n ]
12
+
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def buildTree (self , preorder : List [int ], inorder : List [int ]) -> Optional [TreeNode ]:
3
+ if inorder == []:
4
+ return None
5
+
6
+ mid = preorder .pop (0 )
7
+ midIdx = inorder .index (mid )
8
+ left = self .buildTree (preorder , inorder [:midIdx ])
9
+ right = self .buildTree (preorder , inorder [midIdx + 1 :])
10
+
11
+ return TreeNode (mid , left , right )
12
+
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def numDecodings (self , s : str ) -> int :
3
+ dp = []
4
+ if (s [0 ] == '0' ):
5
+ return 0
6
+ dp .append (1 )
7
+
8
+ for idx , _ in enumerate (s ):
9
+ if idx == 0 :
10
+ continue
11
+ if s [idx ] == '0' :
12
+ if s [idx - 1 ] == '1' or s [idx - 1 ] == '2' :
13
+ if idx == 1 :
14
+ dp .append (1 )
15
+ else :
16
+ dp .append (dp [idx - 2 ])
17
+ else :
18
+ return 0
19
+ elif s [idx - 1 ] == '1' or (s [idx - 1 ] == '2' and (s [idx ] >= '1' and s [idx ] <= '6' )):
20
+ if idx == 1 :
21
+ dp .append (2 )
22
+ else :
23
+ dp .append (dp [idx - 1 ] + dp [idx - 2 ])
24
+ else :
25
+ dp .append (dp [idx - 1 ])
26
+
27
+ return dp [- 1 ]
28
+
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def isAnagram (self , s : str , t : str ) -> bool :
3
+ sLetterDict = {}
4
+ tLetterDict = {}
5
+
6
+ for letter in s :
7
+ sLetterDict [letter ] = sLetterDict .get (letter , 0 ) + 1
8
+ for letter in t :
9
+ tLetterDict [letter ] = tLetterDict .get (letter , 0 ) + 1
10
+
11
+ if len (sLetterDict ) != len (tLetterDict ):
12
+ return False
13
+
14
+ for sKey in sLetterDict .keys ():
15
+ if sKey not in tLetterDict or sLetterDict [sKey ] != tLetterDict [sKey ]:
16
+ return False
17
+
18
+ return True
19
+
You can’t perform that action at this time.
0 commit comments