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. 左端点 小于 中间结点,  左端区间,必为非递减有序。   可判断 target值是否在此区间中。若是,在此区间查找,否则,在另一端点查找。

2. 左端点 大于  中间结点, 则右区间,必为非递减有序。  处理同上。

3. 左端点 等于 中间结点

3.1  此时,如果 右端点 和 中间不相等, 则, 左边区间,所以的值必定一样。  可直接排除点左区间,进入右区间查找。

3.2 三个点都相等, 此时情况不明,无法进行折半。  此时,只能排除掉左端点和右端点,进行小幅度范围缩小。   正是因为有此分支存在,时间复杂度变成了O(n)

class Solution {
public:
    bool search(int A[], int n, int target) {
        int low = 0, high = n-1;
        while (low <= high) {
            const int mid = low + (high - low) / 2;
            if (A[mid] == target)
                return true;
            else if (A[low] < A[mid]) {
                if (A[low] <= target && target < A[mid])
                    high = mid - 1;
                else
                    low = mid + 1;
            }
            else if (A[low] > A[mid]) {
                if (A[mid] < target && target <= A[high])
                    low = mid + 1;
                else
                    high = mid - 1;
            }
            else if (A[low] != A[high])
                low = mid + 1;
            else {
                ++low;
                --high;
            }
        }

        return false;
    }
};
时间: 2024-10-06 18:56:20

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. 这个题做了好长时间,到最后就这个想法... 后来看到别人真的做出来了,我于是又开始新的征程

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