Relative Sort Array
简单的counting题.
class Solution {
public static int[] relativeSortArray(int[] arr1, int[] arr2) {
int[] count = new int[1001];
for(int i = 0; i < arr1.length; i++) { // count all numbers in arr1
count[arr1[i]]++;
}
int[] res = new int[arr1.length];
int t = 0;
for(int i = 0 ; i < arr2.length; i++) { // put all numbers which appears in arr2
int j = count[arr2[i]];
int n = arr2[i];
while(j > 0) {
res[t++] = n;
j--;
count[arr2[i]]--;
}
}
for(int i = 0 ; i < count.length; i++) { // reminding numbers
if(count[i] != 0) {
int j = count[i];
while(j > 0) {
res[t++] = i;
j--;
}
}
}
return res;
}
}