leetcode 704题解
class Solution {
public int search(int[] nums, int target) {
int left = 0;//左界限
int right = nums.length-1;//右界限
while(left <= right){
int mid = (left + right)/2;//二分取中间
//如果目标比中间值大,缩短左边界
if(target > nums[mid]) left = mid + 1;
//如果目标比中间值小,缩短右边界
else if(target < nums[mid]) right = mid - 1;
//找到目标返回
else if(target == nums[mid]) return mid;
}
return -1;
}
}