二分查找
-
二分查找法
注意求mid时的“/“号,是求商后向左一位(则不用考虑mid是否为整数的问题)code_text ```int search(int* nums, int numsSize, int target){ int left = 0, right = numsSize- 1; while(left <= right){ int mid = (right - left) / 2 + left; int num = nums[mid]; if (num == target) { return mid; } else if (num > target) { right = mid - 1; } else { left = mid + 1; } } return -1; }