34. 在排序数组中查找元素的第一个和最后一个位置

这篇博客介绍了如何在排序数组中查找元素的第一个和最后一个位置,提供了两种方法:一是使用双指针的BP算法,二是通过二分查找优化。作者在实践二分查找时遇到了错误,经过复习得以解决。博客强调了对双指针和二分查找技巧的熟练掌握,鼓励读者一起探讨。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

34. 在排序数组中查找元素的第一个和最后一个位置(两种方法记录)

法一(BP算法——使用双指针分别从前、后定位first index和last index),代码如下:

class Solution {
    public int[] searchRange(int[] nums, int target) {
        // the first method
        int[] index = new int[]{-1,-1};
        if(nums.length==0)return new int[]{-1,-1};
        else{
            int slow,fast;
            for(slow=0;slow<nums.length;slow++){
                if(nums[slow]==target){
                    index[0] = slow; // find the first index
                    for(fast=nums.length-1;fast>=slow;fast--){
                        if(nums[fast]==target){
                            index[1] = fast; // find the second index
                            break;
                        }//else if(nums[fast]<){} impossible
                    }
                    break; // important
                }else if(nums[slow]>target)break;
            }
            
        }
        return index;
     }
}

在这里插入图片描述

法一由于使用遍历,可能存在遍历整个数组的可能,所以时间复杂度较高,所以有了法二,即使用二分查找来实现,代码如下:

class Solution {
    public int[] searchRange(int[] nums, int target) {
		// the second method
        int[] index = new int[]{-1,-1};
        if(nums.length==0)return new int[]{-1,-1};
        else{
            int slow,fast;
            for(slow=0;slow<nums.length;slow++){
                if(nums[slow]==target){
                    index[0] = slow; // find the first index
                    
                    // 使用二分查找
                    int half=(slow+nums.length-1)/2;
                    if(half==0)index[1]=0;
                    int start = slow;
                    int end = nums.length-1;
                    while(start<=end){
                        // if(start=end-1){
                        //     if(nums[])
                        // }
                        if(nums[half]>=target){
                            if(nums[half]==target){
                                index[1]=half;
                                start = half+1;
                                half = (start + end)/2;
                            }
                            else{
                                end=half-1;
                                half = (start+end)/2;
                            }

                        }else{
                            end = half-1;
                            half = (start+end)/2;
                        }
                    }

                    if(index[0]!=-1&&index[1]==-1)index[1]=index[0];
                    
                    // for(fast=nums.length-1;fast>=slow;fast--){
                    //     if(nums[fast]==target){
                    //         index[1] = fast; // find the second index
                    //         break;
                    //     }//else if(nums[fast]<){} impossible
                    // }
                    break; // important
                }else if(nums[slow]>target)break;
            }
            
        }
        return index;
    }
}
法二写了很久,后来才发现问题——我错误地使用for循环实现二分查找,而不是使用while循环实现,另外二分查找边界也有问题,没有使用start=half+1和end=half-1而使用start=half和end=half,GG了,后来查询了复习了二分查找知识,才发现问题…

在这里插入图片描述

12-19 第一次温习该案例,使用暴力解法

class Solution {
    public int[] searchRange(int[] nums, int target) {
        // the first go over: BP METHOD
        if(nums.length==0)return new int[]{-1,-1};
        if(nums.length == 1){
            if(nums[0] == target){
                return new int[]{0,0};
            }else{
                return new int[]{-1, -1};
            }
        }
        if(nums[0]>target || nums[nums.length-1]<target)return new int[]{-1, -1};
        int left = 0;
        int right = nums.length - 1;
        int []ans = new int[2];
        while(left <= right){
            if(nums[left] != target && nums[right] != target){
                left++;
                right--;
            }else if(nums[left] != target){
                left++;
            }else if(nums[right] != target){
                right--;
            }else if(nums[left] == target && nums[right] == target){
                break;
            }
        }
        if(left > right)return new int[]{-1, -1};
        ans[0] = left;
        ans[1] = right;
        return ans;
    }
}

在这里插入图片描述

问题:对双指针和二分查找还是不熟练,甚至不敢使用,得继续加油!

最后欢迎各位热爱做题的朋友一起讨论, 😃

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值