Every day a leetcode
题目来源:704. 二分查找
解法1:二分查找
简单的二分查找模板。
代码:
int search(int* nums, int numsSize, int target){
int left=0;
int right=numsSize-1;
int mid;
while(left<=right)
{
mid=(left+right)/2;
if(nums[mid]>target) right=mid-1;
else if(nums[mid]<target) left=mid+1;
else return mid;
}
return -1;
}
结果: