我给跪了orz
class Solution {
public int[] relativeSortArray(int[] arr1, int[] arr2) {
int[] array = new int[arr1.length];
int count = 0;
HashMap<Integer,HashSet<Integer>> hashMap = new HashMap<>();
for(int i = 0;i < arr1.length;i++){
if(!hashMap.containsKey(arr1[i])){
hashMap.put(arr1[i],new HashSet<Integer>());
}
HashSet<Integer> hashSet = hashMap.get(arr1[i]);
hashSet.add(i);
hashMap.put(arr1[i],hashSet);
}
for(int i = 0;i < arr2.length;i++){
HashSet<Integer> hashSet = hashMap.get(arr2[i]);
Iterator<Integer> iterator= hashSet.iterator();
while(iterator.hasNext()){
array[count++] = arr1[iterator.next()];
}
hashMap.remove(arr2[i]);
}
ArrayList<Integer> arr = new ArrayList<>();
for(Map.Entry<Integer,HashSet<Integer>> entry:hashMap.entrySet()){
Iterator<Integer> iterator1 = entry.getValue().iterator();
while(iterator1.hasNext()){
arr.add(arr1[iterator1.next()]);
}
}
arr.sort(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1-o2;
}
});
int i =0 ;
while(i < arr.size()){
array[count++] = arr.get(i++);
}
return array;
}
}