力扣34在排序数组中查找元素的第一个和最后一个位置:思路分析+图文详解+代码实现(最靠左索引,最靠右索引)

简介: 力扣34在排序数组中查找元素的第一个和最后一个位置:思路分析+图文详解+代码实现(最靠左索引,最靠右索引)

第一部分:题目描述

🏠 链接:34. 在排序数组中查找元素的第一个和最后一个位置 - 力扣(LeetCode)

⭐ 难度:中等

第二部分:思路分析

需要使用 最靠左索引最靠右索引 解决该问题。

  • 最靠左索引:搜索目标值为 target 且 索引最小的索引位置
  • 最靠右索引:搜索目标值为 target 且 索引最大的索引位置

📝 以 最靠左索引 为例来说明:

  • 我们知道普通的二分查找是找到一个 目标值 就停止搜索,返回 中间索引 就可以了。
  • 但这里我们需要查找 索引最小的索引位置,那么即使查找到了一个目标值不能直接返回,应该记录下当前的目标值对应的索引,即中间索引mid。

  • 同时,查找到了这个目标值,我们应该缩小搜索的右边范围,既查找 当前mid索引 左边的范围。
  • 即 需要设置 right = mid - 1

同理,最靠右索引也需要记录 每次查找到的索引,同时缩小搜索的左边范围,即设置 left = mid + 1

第三部分:代码实现

class Solution {
    public int[] searchRange(int[] nums, int target) {
        // 查找最小索引
        int minIndex = searchMinIndex(nums, target);
        // 如果未搜索到说明数组不存在目标值
        if (minIndex == -1) {
            return new int[]{-1, -1};
        }
        // 查找最大索引
        int maxIndex = searchMaxIndex(nums, target);
        return new int[]{minIndex, maxIndex};
    }
    private int searchMinIndex(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        // 记录最小索引
        int minIndex = -1;
        int mid;
        while (left <= right) {
            // 无符号右移 代替 /2 操作,避免 left + right 的超出 int能取的最大值
            mid = (left + right) >>> 1;
            if (target < nums[mid]) {
                // 如果 目标值 小于 中间值
                right = mid - 1;
            } else if (nums[mid] < target) {
                // 如果 目标值 大于 中间值
                left = mid + 1;
            } else {
                // 记录当前中间索引为最小索引
                minIndex = mid;
                // 缩小右范围,以便能向左查找更小的索引
                right = mid - 1;
            }
        }
        return minIndex;
    }
    private int searchMaxIndex(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        // 记录最大索引
        int maxIndex = -1;
        int mid;
        while (left <= right) {
            // 无符号右移 代替 /2 操作,避免 left + right 的超出 int能取的最大值
            mid = (left + right) >>> 1;
            if (target < nums[mid]) {
                // 如果 目标值 小于 中间值
                right = mid - 1;
            } else if (nums[mid] < target) {
                // 如果 目标值 大于 中间值
                left = mid + 1;
            } else {
                // 记录当前中间索引为最大索引
                maxIndex = mid;
                // 缩小左范围,以便能向右查找更大的索引
                left = mid + 1;
            }
        }
        return maxIndex;
    }
}
public int[] searchRange(int[] nums, int target) {
        // 查找最小索引
        int minIndex = searchMinIndex(nums, target);
        // 如果未搜索到说明数组不存在目标值
        if (minIndex == -1) {
            return new int[]{-1, -1};
        }
        // 查找最大索引
        int maxIndex = searchMaxIndex(nums, target);
        return new int[]{minIndex, maxIndex};
    }
    private int searchMinIndex(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        // 记录最小索引
        int minIndex = -1;
        int mid;
        while (left <= right) {
            // 无符号右移 代替 /2 操作,避免 left + right 的超出 int能取的最大值
            mid = (left + right) >>> 1;
            if (target < nums[mid]) {
                // 如果 目标值 小于 中间值
                right = mid - 1;
            } else if (nums[mid] < target) {
                // 如果 目标值 大于 中间值
                left = mid + 1;
            } else {
                // 记录当前中间索引为最小索引
                minIndex = mid;
                // 缩小右范围,以便能向左查找更小的索引
                right = mid - 1;
            }
        }
        return minIndex;
    }
    private int searchMaxIndex(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        // 记录最大索引
        int maxIndex = -1;
        int mid;
        while (left <= right) {
            // 无符号右移 代替 /2 操作,避免 left + right 的超出 int能取的最大值
            mid = (left + right) >>> 1;
            if (target < nums[mid]) {
                // 如果 目标值 小于 中间值
                right = mid - 1;
            } else if (nums[mid] < target) {
                // 如果 目标值 大于 中间值
                left = mid + 1;
            } else {
                // 记录当前中间索引为最大索引
                maxIndex = mid;
                // 缩小左范围,以便能向右查找更大的索引
                left = mid + 1;
            }
        }
        return maxIndex;
    }


相关文章
|
5月前
【力扣】-- 移除链表元素
【力扣】-- 移除链表元素
56 1
|
5月前
【LeetCode 27】347.前k个高频元素
【LeetCode 27】347.前k个高频元素
59 0
|
5月前
【LeetCode-每日一题】 删除排序数组中的重复项
【LeetCode-每日一题】 删除排序数组中的重复项
39 4
|
5月前
|
索引
Leetcode第三十三题(搜索旋转排序数组)
这篇文章介绍了解决LeetCode第33题“搜索旋转排序数组”的方法,该问题要求在旋转过的升序数组中找到给定目标值的索引,如果存在则返回索引,否则返回-1,文章提供了一个时间复杂度为O(logn)的二分搜索算法实现。
43 0
Leetcode第三十三题(搜索旋转排序数组)
|
5月前
|
算法 C++
Leetcode第53题(最大子数组和)
这篇文章介绍了LeetCode第53题“最大子数组和”的动态规划解法,提供了详细的状态转移方程和C++代码实现,并讨论了其他算法如贪心、分治、改进动态规划和分块累计法。
97 0
|
5月前
|
C++
【LeetCode 12】349.两个数组的交集
【LeetCode 12】349.两个数组的交集
35 0
|
5月前
【LeetCode 06】203.移除链表元素
【LeetCode 06】203.移除链表元素
45 0
|
6月前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
7月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
80 6
|
7月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
162 2