leetcode:Search in Rotated Sorted Array II(duplicated)

题目要求:

Follow up for "Search in Rotated Sorted Array":
What if duplicates are allowed?

Would this affect the run-time complexity? How and why?

Write a function to determine if a given target is in the array.

题目地址:https://leetcode.com/problems/search-in-rotated-sorted-array-ii/

public class Solution {
    public static boolean search(int[] A, int target) {
        int first=0,last=A.length-1,mid;
        while(first<=last){
            mid=(first+last)/2;
            System.out.println("当前搜索范围"+A[first]+"--"+A[last]+",mid="+A[mid]);
            if(A[mid]==target)return true;
            else if(A[first]<A[mid]){//左有序

                if(target>=A[first]&&target<A[mid]){
                    last=mid-1;
                }else{
                    first=mid+1;
                }
            }else if(A[first]>A[mid]){//右有序
                if(target>A[mid]&&target<=A[last]){
                    first=mid+1;
                }else{
                    last=mid-1;
                }

            }else{//A[first]==A[mid]
                if(A[last]>A[mid]){//右有序
                    if(target>A[mid]&&target<=A[last]){
                        first=mid+1;
                    }else{
                        last=mid-1;
                    }
                }else if(A[last]<A[mid]){//左有序,且左范围为同一数字
                    first=mid+1;

                }else{//无法确定
                    first++;
                    last--;
                }
            }
        }
        return false;
    }
}

分析思路:

首先参照原题没重复项的思路,仅仅是first==mid的时候处理不同,分为下列情况:

在first==mid时:

a.当last>mid,则必有右有序;

b.当last<mid,则翻转奇异点在右范围,则必有左有序,且左范围均为同一个数字。

b.当last=mid时,无法确定奇异点分布,则last,mid,first3个相同均不等于target,则可以last--,first++再看下一步。

时间: 2024-09-30 18:55:02

leetcode:Search in Rotated Sorted Array II(duplicated)的相关文章

【leetcode】Search in Rotated Sorted Array II(middle)☆

Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 我的思路: 太混乱了 不提了.注意关键区分依据 排好序的一定是从小到大的 看大神的吧: b

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

LeetCode OJ:Search in Rotated Sorted Array II(翻转排序数组的查找)

Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 这个和前面的一个不一样就在于可能会有重复的数字,那么判断的时候就应该注意了,遇到start

[LeetCode] Search in Rotated Sorted Array II [36]

题目 Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 原题链接(点我) 解题思路 这题和Search in Rotated Sorted

[leetcode]Search in Rotated Sorted Array II @ Python

原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ 题意: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if

LeetCode——Search in Rotated Sorted Array II

Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 原题链接:https://oj.leetcode.com/problems/search

LeetCode: Search in Rotated Sorted Array II [081]

[题目] Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. [题意] 在"Search in Rotated Sorted Ar

LeetCode: Search in Rotated Sorted Array II 解题报告

Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the arr

leetcode_81——Search in Rotated Sorted Array II(二分查找)

Search in Rotated Sorted Array II Total Accepted: 38274 Total Submissions: 121824My Submissions Question Solution Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and w