题解 | #二分查找-I#
二分查找-I
https://www.nowcoder.com/practice/d3df40bd23594118b57554129cadf47b
int search(int* nums, int numsLen, int target ) {
// write code here
if(numsLen==0)
return -1;
int low=0,mid=numsLen/2,high=numsLen-1;
while(low<=high)
{
if(nums[mid]==target)
return mid;
else if(nums[mid]<target)
{
low=mid+1;
mid=(high+low)/2;
}
else
{
high=mid-1;
mid=(high+low)/2;
}
}
return -1;
}

查看29道真题和解析