​int middle = left + ((right - left) / 2);// 防止溢出 等同于(left + right)/2​

​第一个，最基本的二分查找算法：​

​第二个，寻找左侧边界的二分查找：​

​第三个，寻找右侧边界的二分查找：​

class Solution {  public int[] searchRange(int[] nums, int target) {    int res[] = {-1,-1};    int l = 0, r = nums.length;    res[0] = findLeftBound(l, r, nums, target);    res[1] = findRightBound(l, r, nums, target);    return res;  }  private int findRightBound(int l, int r, int[] nums, int target) {    int mid = (l + r) / 2;    while (l < r) {      mid = (l + r) / 2;      if (nums[mid] <= target)        l = mid + 1;      else        r = mid;    }    if (l  == 0) return -1;    return nums[l - 1] == target ? l-1 : -1;  }  private int findLeftBound(int l, int r, int[] nums, int target) {    int mid = (l + r) / 2;    while (l < r) {      mid = (l + r) / 2;      if (nums[mid] >= target)        r = mid;      else        l = mid + 1;    }    // target 比所有数都大    if (l  == nums.length) return -1;    // 类似之前算法的处理方式    return nums[l ] == target ? l  : -1;  }}

​边界问题​

​大于等于​