更新时间:2022-02-02 09:00:36
您可以在O(n)的做到这一点。遍历数组和计算所有数字的总和。现在,自然数之和从1到N,可以pssed为 EX $ P $ NX(N + 1)/ 2
。在你的情况下,N = 100。
You can do this in O(n). Iterate through the array and compute the sum of all numbers. Now, sum of natural numbers from 1 to N, can be expressed as Nx(N+1)/2
. In your case N=100.
从减去阵列的总和NX(N + 1)/ 2
,其中N = 100。
Subtract the sum of the array from Nx(N+1)/2
, where N=100.
这是缺少的数。可以在其中的总和计算的迭代过程中被检测到的空槽。
That is the missing number. The empty slot can be detected during the iteration in which the sum is computed.
// will be the sum of the numbers in the array.
int sum = 0;
int idx = -1;
for (int i = 0; i < arr.length; i++)
{
if (arr[i] == 0)
{
idx = i;
}
else
{
sum += arr[i];
}
}
// the total sum of numbers between 1 and arr.length.
int total = (arr.length + 1) * arr.length / 2;
System.out.println("missing number is: " + (total - sum) + " at index " + idx);