Skip to content

Commit bd0b701

Browse files
committed
2019-07-14
1 parent aaf1e45 commit bd0b701

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution(object):
2+
def relativeSortArray(self, arr1, arr2):
3+
"""
4+
:type arr1: List[int]
5+
:type arr2: List[int]
6+
:rtype: List[int]
7+
"""
8+
from collections import defaultdict
9+
res = []
10+
left = [] #存放没有在arr2里出现过的数字
11+
set2 = set(arr2) #把arr2转成set,让查找的时间复杂度降低到O(1)
12+
13+
dic = defaultdict(int) #记录arr1中数字出现的频率
14+
for num in arr1:
15+
if num in set2:
16+
dic[num] += 1
17+
else:
18+
left.append(num)
19+
20+
for num in arr2:
21+
res += [num] * dic[num]
22+
23+
left.sort() #按照题目要求把没出现过的元素排序
24+
return res + left
25+
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def lcaDeepestLeaves(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: TreeNode
13+
"""
14+
def inorder(node):
15+
if not node:
16+
return []
17+
return inorder(node.left) + [node.val] + inorder(node.right)
18+
# print inorder(root)
19+
if not root or (not root.left and not root.right):
20+
return root
21+
22+
dic = {}
23+
def depth(node, tmp):
24+
if not node:
25+
return
26+
dic[node] = tmp + 1
27+
depth(node.left, tmp + 1)
28+
depth(node.right, tmp + 1)
29+
30+
depth(root, 0)
31+
# print dic
32+
l = []
33+
max_depth = max(dic.values())
34+
for key, val in dic.items():
35+
if val == max_depth:
36+
l.append(key)
37+
38+
if len(l) == 1:
39+
return l[0]
40+
# print l
41+
tmpAncestor = None
42+
if len(l) >= 2:
43+
tmpAncestor = self.lowestCommonAncestor(root, l[0], l[1])
44+
for i in range(2, len(l), 1):
45+
tmpAncestor = self.lowestCommonAncestor(root, tmpAncestor, l[i])
46+
return tmpAncestor
47+
48+
49+
50+
def lowestCommonAncestor(self, root, p, q):
51+
"""
52+
:type root: TreeNode
53+
:type p: TreeNode
54+
:type q: TreeNode
55+
:rtype: TreeNode
56+
"""
57+
58+
if not root or root == p or root == q:
59+
return root
60+
else:
61+
left = self.lowestCommonAncestor(root.left, p, q)
62+
right = self.lowestCommonAncestor(root.right, p, q)
63+
64+
if left and right: #一个在左子树,一个在右子树
65+
return root
66+
elif left:#都在左子树
67+
return left
68+
elif right:#都在右子树
69+
return right
70+
else:
71+
return
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution(object):
2+
def longestWPI(self, hours):
3+
"""
4+
:type hours: List[int]
5+
:rtype: int
6+
"""
7+
n = len(hours)
8+
a = [0]*n
9+
for i,hour in enumerate(hours):
10+
if hour>8:
11+
a[i]=1
12+
else:
13+
a[i]=-1
14+
for i in range(1,n):
15+
a[i]+=a[i-1]
16+
b={}
17+
res = 0
18+
for i in range(n):
19+
if a[i]>0:
20+
res = max(res,i+1)
21+
else:
22+
if a[i]-1 in b:
23+
res = max(res,i-b[a[i]-1])
24+
if a[i] not in b:
25+
b[a[i]]=i
26+
return res
27+

0 commit comments

Comments
 (0)