假设缺少的数字是 ,而重复的数字是 。如果将所有数字相加,则总和将为:x
y
(n - 1) * n / 2 - x + y
从上面,你可以找到.....(1)(x - y)
同样,对数字的平方求和。然后,总和将为:
(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]