题目如下:

解题思路:题目很简单,求出arr1和arr2的交集以及交集中每个元素出现的次数,按元素在arr2中的顺序排列好,最后再加上arr1中不在arr2里面的元素即可。

代码如下:

class Solution(object):
def relativeSortArray(self, arr1, arr2):
"""
:type arr1: List[int]
:type arr2: List[int]
:rtype: List[int]
"""
dic = {}
for i in arr2:
dic[i] = 1 not_in_2 = []
dic1 = {}
for i in arr1:
if i in dic:
dic1[i] = dic1.setdefault(i,0) + 1
else:
not_in_2.append(i)
res = []
for i in arr2:
res += [i] * dic1[i]
return res + sorted(not_in_2)
05-11 22:37