思路:
1、设置res=[],extra[];
2、遍历arr2,统计arr1中arr2[i]的个数num;
3、在res中追加num个arr2[i];
4、遍历arr1,将没出现在arr2中的元素添加到extra中;
5、将extra升序排列,返回res+extra。
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 res = [] 9 extra = [] 10 # 遍历arr2,统计arr1中arr2[i]的个数num,在res中追加num个arr2[i]; 11 for ch in arr2: 12 num = arr1.count(ch) 13 res += [ch] * num 14 # 遍历arr1,将没出现在arr2中的元素添加到extra中 15 for ch in arr1: 16 if ch not in arr2: 17 extra.append(ch) 18 # 将extra升序排列 19 extra.sort() 20 return res + extra 21 22 23 if __name__ == '__main__': 24 solution = Solution() 25 print(solution.relativeSortArray(arr1=[2, 3, 1, 3, 2, 4, 6, 7, 9, 2, 19], arr2=[2, 1, 4, 3, 9, 6]))