水下功夫做透,水上才能顺风顺水。

有序数组左边最大值

一个有序数组[1,2,3,3,5,6,10,19]

输出->输入数字的左边最大值

比如: 

3->2

19->10 

11->10

 

public Integer search(Integer[] nums, Integer target) {
         if(target<nums[0]){
             return null;
         }
         int high = nums.length-1;
         if(target>nums[high]){
             return a[high];
         }
         int low = 0;
         while(low<=high){
             int mid = (low+high)/2;
             if(target>nums[mid]){
                if(target<nums[mid+1]){
                     return nums[mid];
                }else{
                     low = mid+1;
                 }

             }else if(target<nums[mid]){
                if(target>nums[mid-1]){
                     return nums[mid-1];
                }else{
                     high = mid-1;
                 }

             }else{
                if(a[mid-1]!=target){
                    return a[mid-1];
                }else{
                    high = mid -1;
                }

             }

         }

    }

 

posted @ 2022-09-27 20:16  北方寒士  阅读(38)  评论(0编辑  收藏  举报