二分查找模版

Posted zhihaospace

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二分查找模版相关的知识,希望对你有一定的参考价值。

二分查找模版

  • 来源:公众号 labuladong

  • 注意: int mid = left + (right - left) / 2; 这么写而不是 int mid = (left + right) >> 1; 是由于left加right可能整数溢出

public class BinarySearch {
    
    ////找到nums数组中的一个和target值相同的位置,未找到则返回-1(适用于非重复值数组)
    int binary_search(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) {
                right = mid - 1;
            } else if(nums[mid] == target) {
                // 直接返回
                return mid;
            }
        }
        // 直接返回
        return -1;
    }

    //找到nums数组中最左边一个和target值相同的位置,未找到则返回-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) {
                left = mid + 1;
            } else if (nums[mid] > target) {
                right = mid - 1;
            } else if (nums[mid] == target) {
                // 别返回,收缩左侧边界
                right = mid - 1;
            }
        }
        // 最后要检查 left 越界的情况
        if (left >= nums.length || nums[left] != target)
            return -1;
        return left;
    }


    //找到nums数组中最右边一个和target值相同的位置,未找到则返回-1,(适用于重复值数组)
    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) {
                right = mid - 1;
            } else if (nums[mid] == target) {
                // 别返回,收缩右侧边界
                left = mid + 1;
            }
        }
        // 最后要检查 right 越界的情况
        if (right < 0 || nums[right] != target)
            return -1;
        return right;
    }
    
}
  • 例题:https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/

技术图片

public class P34FindFirstAndLastPositionOfElementInSortedArray {
    /**
     * 输入: nums = [5,7,7,8,8,10], target = 8
     * 输出: [3,4]
     */
    public static void main(String[] args) {
        Solution solution = new P34FindFirstAndLastPositionOfElementInSortedArray().new Solution();
        // TO TEST
        int[] ints = solution.searchRange(new int[]{5, 7, 7, 8, 8, 10}, 8);
        for (int anInt : ints) {
            System.out.print(anInt + ",");
        }
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int[] searchRange(int[] nums, int target) {

            int[] arr = new int[]{-1, -1};
            int len = nums.length;
            if (len == 0) {
                return arr;
            }

            arr[0] = left_bound(nums, target);
            arr[1] = right_bound(nums, target);
            return arr;
        }

        //找到nums数组中最左边一个和target值相同的位置,未找到则返回-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) {
                    left = mid + 1;
                } else if (nums[mid] > target) {
                    right = mid - 1;
                } else if (nums[mid] == target) {
                    // 别返回,收缩左侧边界
                    right = mid - 1;
                }
            }
            // 最后要检查 left 越界的情况
            if (left >= nums.length || nums[left] != target)
                return -1;
            return left;
        }


        //找到nums数组中最右边一个和target值相同的位置,未找到则返回-1,(适用于重复值数组)
        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) {
                    right = mid - 1;
                } else if (nums[mid] == target) {
                    // 别返回,收缩右侧边界
                    left = mid + 1;
                }
            }
            // 最后要检查 right 越界的情况
            if (right < 0 || nums[right] != target)
                return -1;
            return right;
        }
    }
}

 

以上是关于二分查找模版的主要内容,如果未能解决你的问题,请参考以下文章

二分查找模版

一网打尽!二分查找解题模版与题型全面解析

leetcode 69题 思考关于二分查找的模版

各种类型的二分查找模版,我都整理好了

二分查找算法算法指导 意境级讲解

震惊!这些题也能用二分查找做