排序邻数差值 发表于 2018-10-04 | 分类于 数据结构与算法 , 题目汇总 简述求数组排序后相邻数的最大差值 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748public static int maxGap(int[] arr){ if(arr == null || arr.length == 0){ return 0; } int len = arr.length; int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; for(int i = 0; i <len; i++){ max = Math.max(arr[i], max); min = Math.min(arr[i], min); } if(max == min){ return 0; } int[] maxArr = new int[len+1]; int[] minArr = new int[len+1]; boolean[] hasNum = new boolean[len + 1]; int k = 0; for(int i = 0; i < len; i++){ k = getBucket(arr[i], max, min, len); maxArr[k] = hasNum[k] ? Math.max(arr[i], maxArr[k]) : arr[i]; minArr[k] = hasNum[k] ? Math.min(arr[i], minArr[k]) : arr[i]; hasNum[k] = true; } int lastMax = 0; int nextMin = 0; int i = 0; for(; i<len; i++){ if(!hasNum[i]){ lastMax = maxArr[i-1]; break; } } while(i<len+1){ if(hasNum[i]){ nextMin = minArr[i]; break; } i++; } return nextMin - lastMax;}/** * 计算进入哪个桶 max直接放入n+1桶中 */public static int getBucket(int num, int max, int min, int len){ return ((num - min) * len) / (max - min);}