题目链接:https://leetcode-cn.com/problems/relative-sort-array/
题目如下:
class Solution {
public:
vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
sort(arr1.begin(),arr1.end());
map<int,int> umap;//需要顺序,则用map
//需要次数,也需要元素
for(auto e:arr1){
umap[e]++;
}
//把出现的先按顺序放入
vector<int> res;
for(auto e:arr2){
while(umap[e]!=0){//重要,可能出现重复元素
umap[e]--;
res.push_back(e);
}
}
//把剩余的放在末尾
for(auto e:umap){
while(e.second!=0){//重要,可能出现重复元素
e.second--;
res.push_back(e.first);
}
}
return res;
}
};