Search in Rotated Sorted Array II - Leetcode

本文介绍了一个针对含有重复元素的旋转有序数组进行搜索的算法。该算法通过递归地将问题分解为子问题来查找目标值,并详细解释了如何处理数组中出现的重复元素的情况。

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

<p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 30px;">Follow up for "Search in Rotated Sorted Array":<br style="box-sizing: border-box;" />What if <span style="box-sizing: border-box;">duplicates</span> are allowed?</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 30px;">Would this affect the run-time complexity? How and why?</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 30px;">Write a function to determine if a given target is in the array.</p>
----------------------------------------------------------------------
public class Solution {
    public boolean search(int[] A, int target) {
        if(A.length < 1)
        return false;
        if(A.length < 2)
        return A[0] == target;
        
        int low=0, high=A.length-1, mid=(low+high)/2;
        while(low <= high){
            mid=(low+high)/2;
            if(A[mid] == target)
            return true;
            if(A[mid] > A[low]){
                if(A[low]<=target && target<A[mid])
                high=mid;
                else
                low=mid+1;
            }else if(A[mid] < A[low]){
                if(A[mid]<target && target<=A[high])
                low=mid+1;
                else
                high=mid;
            }else
            low ++;
        }
        return false;
    }
}
思路:不多说了看

Search in Rotated Sorted Array。

tricky的内容就是如果遇到一样的low++知道遇到不同的数字

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值