当年面试时听说过的一个题目,不过本人没有遇到,也就没太在意。今天忽然在一个论坛看到这个题目,就做了一下。
题目:用最少的循环找出2个已排序的数组中相同数字
int[] a=new int[]{1,3,5,7,9,10};
int[] b=new int[]{2,4,6,8,11,13};
int aIndex=0;
int bIndex=0;
boolean isEqualNumber=false;
while(aIndex<a.length&&bIndex<b.length)
{
if(a[aIndex]<b[bIndex])
{
aIndex++;
}
else if(a[aIndex]>b[bIndex])
{
bIndex++;
}
else
{
isEqualNumber=true;
break;
}
}
if(isEqualNumber)
{
System.out.println("2个数组有相同的数字!");
}
else
{
System.out.println("2个数组没有相同的数字!");
}
面试题目
最新推荐文章于 2025-02-18 22:13:45 发布