704. 二分查找
704. 二分查找 - 力扣(LeetCode) (leetcode-cn.com)
class Solution {
public int search(int[] nums, int target) {
int l = 0, r = nums.length-1;
while(l<=r){
int mid = l + ((r - l) / 2);// 防止溢出 等同于(l + r)/2
if(nums[mid]==target){
return mid;
}else if(nums[mid]>target){
r = mid-1;
}else{
l = mid+1;
}
}
return -1;
}
}
278. 第一个错误的版本
278. 第一个错误的版本 - 力扣(LeetCode) (leetcode-cn.com)
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int l = 1;
while(l<n){
int mid = l+(n-l)/2;
if(isBadVersion(mid)){
n = mid;
}else{
l = mid+1;
}
}
return l;
}
}