class Solution {
public int[] relativeSortArray(int[] arr1, int[] arr2) {
int[] hashArray = new int[1001];
for (int i = 0; i < arr1.length; i++) { // 计数数组
hashArray[arr1[i]]++;
}
int flag = 0;
for (int i = 0;i<arr2.length;i++){ //先按照arr2出现的顺序把arr1前一部分赋值
while (hashArray[arr2[i]]>0){
arr1[flag] = arr2[i];
hashArray[arr2[i]]--;
flag++;
}
}
for (int i = 0;i<hashArray.length;i++){ //剩余元素按顺序加入arr1表尾
while (hashArray[i]>0){
arr1[flag] = i;
hashArray[i]--;
flag++;
}
}
return arr1;
}
}