[leetcode]1331. 数组序号转换

[leetcode]1331. 数组序号转换_第1张图片

class Solution {
public:
    vector<int> arrayRankTransform(vector<int>& arr) {
        int len = arr.size();
        if(len == 0)
        {
            return arr;
        }
        vector<int>copy = arr;
        sort(copy.begin(), copy.end());
        map<int,int>dict;
        int idx = 1;
        for(int i = 0; i < len-1 ; i++)
        {
            if(copy[i] != copy[i+1])
            {
                dict[copy[i]] = idx;
                idx++;
            }
        }
        dict[copy[len-1]] = idx;
        for(int i = 0; i < len; i++)
        {
            arr[i] = dict[arr[i]];
        }
        return arr;
    }
};

你可能感兴趣的:(leetcode,leetcode)