704. 二分查找

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/binary-search
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
给了指定的值,使用left <= right;
public int search(int[] nums, int target) {
int l=0;
int r= nums.length-1;
while(l<=r) {
int mid = l+(r-l)/2;
if(nums[mid] == target) {
return mid;
} else if(nums[mid] > target) {
r= mid-1;
} else {
l = mid+1;
}
}
return -1;
}
浙公网安备 33010602011771号