Search in Rotated Sorted Array II——LeetCode

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.

这个题做了好长时间,到最后就这个想法。。。

后来看到别人真的做出来了,我于是又开始新的征程。。。终于顿悟了。。。

首先:

把左右两边的重复元素都过滤了。

while(lo<hi&&nums[lo]==nums[lo+1])lo++;
while(lo<hi&&nums[hi]==nums[hi-1])hi--;

然后开始思考一下rotated sorted array的特点:

有以下两种情况:

1 2 3 4 5 6 7 8 完全顺序的

5 6 7 8 1 2 3 4 反转的

lo = 0

hi = len - 1

mid = (lo+hi)>>>1

对于完全顺序的不用多说。

对于翻转的,这时mid会有两种情况:

一、nums[mid]>nums[hi]

二、nums[mid]<nums[hi]

ok,情况说明白了,下面来说target对应的情况:

如果target比nums[hi]大,那么在前半部分的情况有:

nums[hi]>nums[mid]或target<nums[mid]

如果target比nums[hi]小,那么在后部分的情况有:

target>nums[mid]或者nums[hi]<nums[mid]

     public boolean search(int[] nums,int target){
        if(nums==null||nums.length==0){
            return false;
        }
        int lo = 0, hi = nums.length-1;
        while(lo<=hi){
            while(lo<hi&&nums[lo]==nums[lo+1])lo++;
            while(lo<hi&&nums[hi]==nums[hi-1])hi--;

            int mid = (lo+hi)>>>1;
            if(target == nums[mid]){
                return true;
            }
            if(target>nums[hi]){
                if(nums[hi]>nums[mid]||target<nums[mid]){
                    hi=mid-1;
                }else{
                    lo=mid+1;
                }
            }
            else{
                if(target>nums[mid]||nums[hi]<nums[mid]){
                    lo=mid+1;
                }else{
                    hi=mid-1;
                }
            }
        }
        return false;
    }
时间: 2024-10-14 14:07:04

Search in Rotated Sorted Array II——LeetCode的相关文章

Search in Rotated Sorted Array II leetcode java

题目: 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 Array

Search in Rotated Sorted Array II -- leetcode

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. 左端点 小于 中间结点,  

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: 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]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 [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 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

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