//递归版二分查找
int binarySearch(vector<int>& nums,int left,int right,int target){
if(nums.empty()) return -1;
int mid=0;
while(left<right){
mid=(left+right)/2;
if(nums[mid]==target)
return mid;
else if(nums[mid]>target)
return binarySearch(nums,left,mid,target);
else
return binarySearch(nums,mid+1,right,target);
}
return -1;
}
//非递归版二分查找
int binarySearch(vector<int>& nums,int left,int right,int target){
if(nums.empty()) return -1;
int mid=0;
while(left<right){
mid=(left+right)/2;
if(nums[mid]==target)
return mid;
else if(nums[mid]>target)
right=mid;
else
left=mid+1;
}
return -1;
}
2