LeetCode945:使数组唯一的最小增量

LeetCode945:使数组唯一的最小增量

LeetCode945:使数组唯一的最小增量

思想:基本程序设计。

AC代码如下:

public int minIncrementForUnique(int[] A) {
        int result = 0;
        Arrays.sort(A);
        for(int i=1;i<A.length;i++) {
            while(A[i]<=A[i-1]) {
                int add = A[i-1]-A[i]+1;
                A[i]+=add;
                result+=add;
            }
        }
        return result;
    }

LeetCode945:使数组唯一的最小增量

 

posted @ 2019-05-15 18:59 godoforange 阅读(...) 评论(...) 编辑 收藏