二分查找-数据结构与算法学习

二分查找笔记

      • 二分查找框架
      • 实际问题

二分查找框架

int binarySearch(int[] nums, int target) {
    int left = 0, right = ...;

    while(...) {
        int mid = left + (right - left)/2;
        if(nums[mid] == target) {
            ...
        } else if(nums[mid] < target) {
            left = ...
        } else if(nums[mid] > target) {
            right = ...
        }
    }
    return ...;
}

技巧:不要出现else, 而是把所有情况用else if写清楚!!!

实际问题

  1. 寻找一个数
int binarySearch(int[] nums, int target) {
    int left = 0;
    int right = nums.length - 1;
    
    while(left <= right) {
        int mid = left + (right - left)/2;
        if(nums[mid] == target) 
            return mid;
        else if(nums[mid] < target)
            left = mid + 1;
        else if(nums[mid] > target)
            right = mid - 1;
    }
    return -1;
}
  1. 寻找左侧边界
int left_bound(int[] nums, int target) {
    if(nums.length == 0) return -1;
    int left = 0;
    int right = nums.length;

    while(left < right){
        int mid = left + (right - left)/2;
        if(nums[mid] == target)
            right = mid;//左闭右开,right不在搜索范围, [left, mid)
        else if(nums[mid] < target)
            left = mid + 1;
        else if(nums[mid] > target)
            right = mid;//右端边界不能取right,左闭右开
    }
    if(left == nums.length) return -1;// 比所有数都大
    return left;
}
  1. 寻找右侧边界
int right_bound(int[] nums, int target) {
    if(nums.length == 0) return -1;
    int left = 0;
    int right = nums.length;

    while(left < right){
        int mid = left + (right - left)/2;
        if(nums[mid] == target)
            left = mid + 1;//[mid+1, right) 
        else if(nums[mid] < target)
            left = mid + 1;
        else if(nums[mid] > target)
            right = mid;
    }
    return left - 1;
}
  1. 统一写法
int binarySearch(int[] nums, int target) {
    int left = 0, right = nums.length - 1;
    while(left <= right){
        int mid = left + (right - left)/2;
        if(nums[mid] == target)
            return mid;
        else if(nums[mid] < target)
            left = mid + 1;
        else if(nums[mid] > target)
            right = mid - 1;
    }
    return -1;
}

int left_bound(int[] nums, int target) {
    int left = 0, right = nums.length - 1;
    while(left <= right){
        int mid = left + (right - left)/2;
        if(nums[mid] == target)
            right = mid - 1;
        else if(nums[mid] < target)
            left = mid + 1;
        else if(nums[mid] > target)
            right = mid - 1;
    }
    //检查越界, 是否所有值均小于target
    if(left >= nums.length || nums[left] != target) return -1;
    return left;
}

int right_bound(int[] nums, int target) {
    int left = 0, right = nums.length - 1;
    while(left <= right){
        int mid = left + (right - left)/2;
        if(nums[mid] == target)
            left = mid + 1;
        else if(nums[mid] < target)
            left = mid + 1;
        else if(nums[mid] > target)
            right = mid - 1;
    }
    //检查越界, 是否所有值均大于target
    if(right < 0 || nums[right] != target) return -1;
    return right;
}

你可能感兴趣的:(数据结构与算法)