int findMaxDifBt2Nums(int* arr, int len) { int maxItem = arr[0], minItem = arr[0]; for (int i = 1; i < len; ++i) { if (arr[i] > maxItem) { maxItem = arr[i]; } if (arr[i] < minItem) { minItem = arr[i]; } } //间距 int bar = (maxItem - minItem) / (len - 1); int (*bucket)[2] = new int[len][2]; memset(bucket, 0, sizeof(int) * len * 2); for (int i = 0; i < len; ++i) { int pos = (arr[i] - minItem) / bar; if (bucket[pos][0] == 0) { bucket[pos][0] = bucket[pos][1] = arr[i]; } else { if (arr[i] > bucket[pos][1]) { bucket[pos][1] = arr[i]; } if (arr[i] < bucket[pos][0]) { bucket[pos][0] = arr[i]; } } } int maxDif = 0; int* tmp = NULL; for (int i = 1; i < len; ++i) { if (bucket[i][0] != 0) { if (!tmp) { tmp = bucket[i]; continue; } maxDif = max(maxDif, bucket[i][0] - tmp[1]); tmp = bucket[i]; } } delete[] bucket; return maxDif; }