码迷,mamicode.com
首页 > 其他好文 > 详细

Search in Rotated Sorted Array II

时间:2020-04-12 14:17:08      阅读:54      评论:0      收藏:0      [点我收藏+]

标签:run   题解   log   缩小   复杂   arch   size   复杂度   false   

Search in Rotated Sorted Array

Problem

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.

题解

仔细分析此题和之前一题的不同之处,前一题我们利用A[start] < A[mid]这一关键信息,而在此题中由于有重复元素的存在,在A[start] == A[mid]时无法确定有序数组,此时只能依次递增start/递减end以缩小搜索范围,时间复杂度最差变为O(n)。

C++

class Solution {
    /**
     * param A : an integer ratated sorted array and duplicates are allowed
     * param target :  an integer to be search
     * return : a boolean
     */
public:
    bool search(vector<int> &A, int target) {
        if (A.empty()) {
            return false;
        }

        vector<int>::size_type start = 0;
        vector<int>::size_type end = A.size() - 1;
        vector<int>::size_type mid;

        while (start + 1 < end) {
            mid = start + (end - start) / 2;
            if (target == A[mid]) {
                return true;
            }
            if (A[start] < A[mid]) {
                // situation 1, numbers between start and mid are sorted
                if (A[start] <= target && target < A[mid]) {
                    end = mid;
                } else {
                    start = mid;
                }
            } else if (A[start] > A[mid]) {
                // situation 2, numbers between mid and end are sorted
                if (A[mid] < target && target <= A[end]) {
                    start = mid;
                } else {
                    end = mid;
                }
            } else  {
                // increment start
                ++start;
            }
        }

        if (A[start] == target || A[end] == target) {
            return true;
        }
        return false;
    }
};

 

Java

public class Solution {
    /**
     * param A : an integer ratated sorted array and duplicates are allowed
     * param target :  an integer to be search
     * return : a boolean
     */
    public boolean search(int[] A, int target) {
        if (A == null || A.length == 0) return false;

        int lb = 0, ub = A.length - 1;
        while (lb + 1 < ub) {
            int mid = lb + (ub - lb) / 2;
            if (A[mid] == target) return true;

            if (A[mid] > A[lb]) {
                // case1: numbers between lb and mid are sorted
                if (A[lb] <= target && target <= A[mid]) {
                    ub = mid;
                } else {
                    lb = mid;
                }
            } else if (A[mid] < A[lb]) {
                // case2: numbers between mid and ub are sorted
                if (A[mid] <= target && target <= A[ub]) {
                    lb = mid;
                } else {
                    ub = mid;
                }
            } else {
                // case3: A[mid] == target
                lb++;
            }
        }

        if (target == A[lb] || target == A[ub]) {
            return true;
        }
        return false;
    }
}

 

源码分析

A[start] == A[mid]时递增start序号即可。

复杂度分析

最差情况下 O(n), 平均情况下 O(logn).

 

Search in Rotated Sorted Array II

标签:run   题解   log   缩小   复杂   arch   size   复杂度   false   

原文地址:https://www.cnblogs.com/lyc94620/p/12684708.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!