面试题目

  当年面试时听说过的一个题目,不过本人没有遇到,也就没太在意。今天忽然在一个论坛看到这个题目,就做了一下。
  题目:用最少的循环找出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个数组没有相同的数字!");
            }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值