class Solution {
public int search(int[] nums, int target) {
int left=0,right=nums.length-1;
return res(nums,target,left,right);
}
int res(int[] nums,int target,int left,int right){
int mid=(left+right)/2;
if(mid>nums.length-1)
return -1;
if(mid<0)
return -1;
if(left>right)
return -1;
if(nums[mid]==target)
return mid;
if(nums[mid]<target){
left=mid+1;
return res(nums,target,left,right);
}
if(nums[mid]>target){
right=mid-1;
return res(nums,target,left,right);
}
return -1;//预防意外情况,不写报错
}
}
标签:二分,right,return,target,nums,int,mid,查找
From: https://blog.csdn.net/Runner__Binger/article/details/141831531