Treearithmetic-二叉树

public class Treearithmetic
{    
      public static void main(String[] args)
    {
       
        String[] a = {"6", "s", "a", "b", "c", "s", "s", "s", "f", "s", "f", "s", "3", "3"};
        String vString = Treearithmetic.find(a, "6", 14);
        String vString2 = Treearithmetic.find2(a, "3", 14);
        System.out.println(vString);
        System.out.println(vString2);
    }
    // 找6 a b
    public static String find(String[] a, String searchString, int nElement)
    {
        int lowerBound = 0;
        int upperBound = nElement - 1;
        while (true)
        {

            //int num=lowerBound+((upperBound-lowerBound)/2);后者超出232溢出成负数
            int num = (lowerBound + upperBound) / 2;

            if (a[num] == searchString)
            {
                return a[num];
            }
            else
            {
                upperBound = num - 1;
            }
        }
    }

    // 找3
    public static String find2(String[] a, String searchString, int nElement)
    {
        int lowerBound = 0;
        int upperBound = nElement - 1;
        while (true)
        {
            int num = (lowerBound + upperBound) / 2;
            if (a[num] == searchString)
            {
                return a[num];
            }
            else
            {
                lowerBound = num + 1;
            }  
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值