LeetCode高频面试60天打卡日记Day22

Day22(数组唯一的最小增量)

在这里插入图片描述

class Solution {
    
    
    public int minIncrementForUnique(int[] A) {
    
    
        //先排序
        Arrays.sort(A);
        int ans = 0;
        for(int i=1;i<A.length;i++){
    
    
            if(A[i]<=A[i-1]){
    
    
                int temp = A[i];
                A[i] = A[i-1]+1;
                ans+=A[i-1]+1-temp;
            }
        }
        return ans;
    }
}

猜你喜欢

转载自blog.csdn.net/YoungNUAA/article/details/105056705