res=-1,首先找到中间的数

  1. nums[mid]==target说明右边都是>=target的数、更新res,right=mid-1
  2. nums[mid]>target说明右边都是>target的数,right=mid-1
  3. nums[mid]<target说明左边都是<target的数,left=mid+1;
    public int search (int[] nums, int target) {
         // write code here
         if(nums.length==0){
             return -1;
         }
         int res=-1;
         int left=0,right=nums.length-1;
         if(nums[left]>target || nums[right]<target ){
             return -1;
         }
         while(left<=right){
             int mid=(right-left)/2+left;
             if(nums[mid]==target){
                 res=mid;
                 right=mid-1;
             }
             else if(nums[mid]>target){
                 right=mid-1;
             }
             else{
                 left=mid+1;
             }
         }
         return res;
     }