Skip to content

Commit 61423c5

Browse files
committed
ngocquan
1 parent 098058c commit 61423c5

File tree

4 files changed

+132
-42
lines changed

4 files changed

+132
-42
lines changed

1. Two Sum.py

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,53 @@
1-
class Solution:
2-
def twoSum(self, nums, target) :
3-
dict_nums = {}
4-
for i in range(len(nums)) :
5-
dict_nums[nums[i]] = i
6-
for i in range(len(nums)):
7-
if target - nums[i] in dict_nums :
8-
j = dict_nums[target - nums[i]]
9-
if i != j :
10-
return [i, j]
1+
# class Solution:
2+
# def twoSum(self, nums, target) :
3+
# dict_nums = {}
4+
# for i in range(len(nums)) :
5+
# dict_nums[nums[i]] = i
6+
# for i in range(len(nums)):
7+
# if target - nums[i] in dict_nums :
8+
# j = dict_nums[target - nums[i]]
9+
# if i != j :
10+
# return [i, j]
11+
12+
13+
14+
class Solution(object):
15+
def twoSum(self, nums, target):
16+
"""
17+
:type nums: List[int]
18+
:type target: int
19+
:rtype: List[int]
20+
"""
21+
set_nums = set(nums)
22+
23+
first_ind = -1
24+
25+
second_ind = -1
26+
27+
for i, num in enumerate(nums) :
28+
if target - num in set_nums :
29+
30+
first_ind = i
31+
32+
second_num = target - num
33+
break
34+
print(first_ind)
35+
# The first num must be found befor the second num
36+
for i, num in enumerate(nums[first_ind+1 :]) :
37+
if num == second_num :
38+
second_ind = i
39+
return [first_ind, second_ind]
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
53+

543. Diameter of Binary Tree.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 __init__(self):
9+
self.max_diameter = 0
10+
11+
def max_depth(self, root):
12+
if not root:
13+
return 0
14+
if root.left == None and root.right == None:
15+
return 1
16+
left_depth = self.max_depth(root.left)
17+
right_depth = self.max_depth(root.right)
18+
current_diameter = left_depth + right_depth
19+
if current_diameter > self.max_diameter:
20+
self.max_diameter = current_diameter
21+
return max(left_depth, right_depth) + 1
22+
23+
def diameterOfBinaryTree(self, root):
24+
max_diameter = 0
25+
self.max_depth(root)
26+
return self.max_diameter

test.py

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,2 @@
1-
from collections import Counter
2-
3-
4-
class Solution:
5-
def isIsomorphic(self, s: str, t: str) -> bool:
6-
transform = dict() # Create transform from s to t
7-
unique = dict()
8-
ans = []
9-
if len(s) != len(t):
10-
return False
11-
n = len(s) # = len(t) also
12-
13-
for i in range(n):
14-
if s[i] not in transform:
15-
transform[s[i]] = t[i]
16-
if t[i] in unique:
17-
return False
18-
unique[t[i]] = None
19-
# If transform[s[i]] exists
20-
if t[i] != transform[s[i]]:
21-
return False
22-
23-
for i in range(n):
24-
ans.append(transform[s[i]])
25-
ans_string = "".join(ans)
26-
print(ans_string)
27-
28-
return ans_string == t
29-
30-
31-
obj = Solution()
32-
print(obj.isIsomorphic("badc", "bada"))
1+
a = [[0 for i in range(5)] for j in range(3)]
2+
print(a)

test1.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
n = int(input())
2+
3+
keys = [int(i) for i in input().split()]
4+
5+
6+
class Node:
7+
def __init__(self, key):
8+
self.key = key
9+
self.left = None
10+
self.right = None
11+
self.child = 0 # total children
12+
13+
14+
def insert(root, key):
15+
if root is None:
16+
return Node(key)
17+
18+
if key < root.key:
19+
root.left = insert(root.left, key) # create left child
20+
else:
21+
root.right = insert(root.right, key)
22+
23+
root.child += 1
24+
25+
return root
26+
27+
28+
root = None
29+
for key in keys:
30+
root = insert(root, key)
31+
32+
33+
def count_balance_node(root):
34+
if root is None or (root.left == None and root.right == None):
35+
return 0
36+
37+
if root.left == None:
38+
return count_balance_node(root.right)
39+
elif root.right == None:
40+
return count_balance_node(root.left)
41+
42+
is_balance = 0
43+
if root.left.child == root.right.child:
44+
is_balance = 1
45+
else:
46+
is_balance = 0
47+
48+
return is_balance + count_balance_node(root.left) + count_balance_node(root.right)
49+
50+
51+
print(count_balance_node(root))

0 commit comments

Comments
 (0)