题目:
题解:
思路:排序+hashmap
- 先拷贝原数组,然后将数组进行排序,使用 hashmap 记录排序后数组中每个数字首次出现的位置,然后再原数组中添加 hashmap 中记录的位置即可。
代码如下:
class Solution {
public:
vector arrayRankTransform(vector& a) {
vector b=a;
sort(b.begin(),b.end());
// 记录排序后的数字首次出现的位置
unordered_map pos;
int idx=0;
for(int x:b){
if(!pos.count(x))pos[x]=++idx;
}
vector res;
// 添加数字x排完序之后的位置
for(int x:a){
res.push_back(pos[x]);
}
return res;
}
};


![[排序+哈希表]leetcode1331:数组序号转换(easy) [排序+哈希表]leetcode1331:数组序号转换(easy)](http://www.mshxw.com/aiimages/31/1032685.png)
