背景:
我有一个 N 长度的正随机数数组,肯定包含重复项。例如 10,4,5,7,10,9,10,9,8,10,5
编辑:N 很可能是 32,或者其他一些关于该大小的 2 的幂。
问题:
我正在尝试找到用 0-(N-1) 中缺失的数字替换重复项的最快方法。使用上面的例子,我想要一个看起来像这样的结果:
10,4,5,7,0,9,1,2,8,3,6
目标是让每个数字中的一个从 0 到 N-1 ,而不只是用 0-(N-1) 替换所有数字(随机顺序很重要)。
编辑:这个替换是确定性的也很重要,即相同的输入将具有相同的输出(不是随机的)。
我的解决方案:
目前在 Java 中实现,使用 2 个布尔数组来跟踪使用/未使用的数字(范围 [0,N) 中的唯一数字/缺失数字),并且具有近似的最坏情况运行时 N+N*sqrt(N) .
代码如下:
public byte[] uniqueify(byte[] input)
{
boolean[] usedNumbers = new boolean[N];
boolean[] unusedIndices = new boolean[N];
byte[] result = new byte[N];
for(int i = 0; i < N; i++) // first pass through
{
int newIdx = (input[i] + 128) % N; // first make positive
if(!usedNumbers[newIdx]) // if this number has not been used
{
usedNumbers[newIdx] = true; // mark as used
result[i] = newIdx; // save it in the result
}
else // if the number is used
{
unusedIndices[i] = true; // add it to the list of duplicates
}
}
// handle all the duplicates
for(int idx = 0; idx < N; idx++) // iterate through all numbers
{
if(unusedIndices[idx]) // if unused
for(int i = 0; i < N; i++) // go through all numbers again
{
if(!usedNumbers[i]) // if this number is still unused
{
usedNumbers[i] = true; // mark as used
result[i] = idx;
break;
}
}
}
return result;
}
这似乎是我所希望的最快的,但我想我会问互联网,因为有比我聪明得多的人可能有更好的解决方案。
注意 建议/解决方案不必使用 Java。
谢谢你。
编辑:我忘了提到我正在将它转换为 C++。我发布了我的 java 实现,因为它更完整。