假设缺少的数字是x
,重复的数字是y
。如果将所有数字相加,总和将是:
(n - 1) * n / 2 - x + y
从以上可以发现(x - y)
.....(1)
同样,对数字的平方求和。那么总和将是:
(n - 1) * n * (2 * n - 1) / 6 - x2 + y2
从上面你得到....(2)(x2 - y2)
(2) / (1) gives (x + y).....(3)
(1) + (3) 给出2 * x
,因此您可以找到x
和y
。
请注意,在这个解决方案中有O(1)
额外的存储空间并且是O(n)
时间复杂度。上面的其他解决方案是不必要O(n)
的额外存储。
混合 C/C++ 中的代码更清晰:
#include <stdio.h>
int findDup(int *arr, int n, int& dup, int& missing)
{
int sum = 0;
int squares = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
squares += arr[i] * arr[i];
}
sum = (n - 1) * n / 2 - sum; // x - y
squares = (n - 1) * n * (2 * (n - 1) + 1) / 6 - squares; // x^2 - y^2
if (sum == 0) {
// no duplicates
missing = dup = 0;
return -1;
}
missing = (squares / sum + sum) / 2; // ((x^2 - y^2) / (x - y) + (x - y)) / 2 = ((x + y) + (x - y)) / 2 = x
dup = missing - sum; // x - (x - y) = y
return 0;
}
int main(int argc, char *argv[])
{
int dup = 0;
int missing = 0;
int a[] = {0, 2, 1, 2, 4};
findDup(a, sizeof(a) / sizeof(int), dup, missing);
printf("dup = [%d], missing = [%d]\n", dup, missing);
int b[] = {3, 0, 0, 4, 2, 1};
findDup(b, sizeof(b) / sizeof(int), dup, missing);
printf("dup = [%d], missing = [%d]\n", dup, missing);
return 0;
}
输出:
dup = [2], missing = [3]
dup = [0], missing = [5]
一些python代码:
def finddup(lst):
sum = 0
sumsq = 0
missing = 0
dup = 0
for item in lst:
sum = sum + item
sumsq = sumsq + item * item
n = len(a)
sum = (n - 1) * n / 2 - sum
sumsq = (n - 1) * n * (2 * (n - 1) + 1) / 6 - sumsq
if sum == 0:
return [-1, missing, dup]
missing = ((sumsq / sum) + sum) / 2
dup = missing - sum
return [0, missing, dup]
found, missing, dup = finddup([0, 2, 1, 2, 4])
if found != -1:
print "dup = " + str(dup) + " missing = " + str(missing)
print finddup([3, 0, 0, 4, 2, 1])
输出:
dup = 2 missing = 3
[-1, 0, 0]