Leetcode Brush Questions-Array Number Conversion

Idea: You can clone an identical array first, and then sort it, using the array value as the key and the serial number as the value into a Map.

answer:

class Solution {
    public int[] arrayRankTransform(int[] arr) {
        int [] temp = arr.clone();
        HashMap<Integer,Integer> map = new HashMap();
        Arrays.sort(temp);
        int count = 1;
        int num = arr.length;
        for(int i = 0;i < num;i++){
            if(map.get(temp[i]) == null){
                map.put(temp[i],count);
                count++;
            }
        }
        for(int j = 0;j < num;j++){
            arr[j] = map.get(arr[j]);
        }
        return arr;
    }
}

 

 

Guess you like

Origin blog.csdn.net/qq_36428821/article/details/113407489