The title

I give you two arrays, arr1 and arR2,

The elements in ARR2 are different and every element in ARR2 appears in ARR1 and sort the elements in ARR1 so that the relative order of the items in ARR1 is the same as the relative order in ARR2. Elements that do not appear in ARR2 need to be placed at the end of ARR1 in ascending order.

 

Example: input: arr1 =,3,1,3,2,4,6,7,9,2,19 [2], arr2 =,1,4,3,9,6 [2] output:,2,2,1,4,3,3,9,6,7,19 [2]Copy the code

Tip:

1 <= arr1.length, arr2.length <= 1000 0 <= arr1[i], Arr2 [I] <= 1000 The element arR2 [I] in ARR2 varies and every element arR2 [I] in ARR2 occurs in ARR1

Their thinking

class Solution: def relativeSortArray(self, arr1: List[int], arr2: List[int]) -> List[int]: retList = [] for i in arr2: While True: try: arr1.remove(I) retlist.append (I) except: arr1.remove(I) retlist.append (I) except: break retList += sorted(arr1) return retList if __name__ == '__main__': Arr1 = [2,3,1,3,2,4,6,7,9,2,19] arr2 = [2,1,4,3,9,6] ret = Solution(). RelativeSortArray (arr1, arr2) print(ret)Copy the code