算法之禅-二分查找
public int Search(int[] nums, int target) {
int left = 0;
int right = nums.Length - 1;
while(left <= right)
{
int middle = left + (right - left)/2;
if (nums[middle]>target)
{
right = middle - 1;
}else if (nums[middle]<target)
{
left = middle + 1;
}else
{
return middle;
}
}
// 未找到目标值
return -1;
}
//注意中间值:left+(right-left)/2 = mid;