给定一个连续数组,找出缺失的数字
没什么好讲的,直接上代码吧。
public class FindLostNo{
public static void main(String[] args) {
Integer[] test1 = {10,11,12,13,14,16,17,19,20};
int[] test2 = null;
test2 = new int[11];
for(int i=10; i<(test2.length+8); i++) {
test2[test1[i-10]-10] = 1;
}
for(int i=0; i<test2.length; i++) {
if(test2[i]==0) {
System.out.println("The lost number is: " + (test2[i]+i+10));
}
}
}
}
运行输出如下:
The lost number is: 15
The lost number is: 18
本文介绍了一种在连续数组中查找缺失数字的算法实现。通过使用整型数组存储已知数字,然后遍历检查未被标记的位置,从而定位丢失的数字。示例代码展示了如何找出10到20之间的缺失数字15和18。
1882

被折叠的 条评论
为什么被折叠?



