码迷,mamicode.com
首页 > 其他好文 > 详细

二分查找模版

时间:2020-04-29 14:23:15      阅读:50      评论:0      收藏:0      [点我收藏+]

标签:重复   一个   ima   溢出   mamicode   region   二分   lse   deletion   

二分查找模版

  • 来源:公众号 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;
        }
    }
}

 

二分查找模版

标签:重复   一个   ima   溢出   mamicode   region   二分   lse   deletion   

原文地址:https://www.cnblogs.com/zhihaospace/p/12801664.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!