leetcode_81_Search in Rotated Sorted Array II

欢迎转载,如有错误或疑问请留言纠正,谢谢微笑


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.

//方法:比较笨的
class Solution {
public:
    bool search(vector<int>& nums, int target) {
        if(nums.size() == 0)
            return false;
        if(nums.size() == 1)
            if(nums[0] == target)
                return true;
        int left = 0;
        int right = nums.size()-1;
        int mid = (right + left) / 2;
        bool ans = false;
        
        while(left < right)
        {
            mid = (right + left) / 2;
            while((nums[left] == nums[right]) || (left != mid && nums[mid] == nums[left]))  //left==right不要也是可以的
            {
                left++;
                mid = (right + left) / 2;
            }
            while(nums[right] == nums[mid])
            {
                right--;
                mid = (right + left) / 2; // return false:[3,1,1], 1
            }
                
            mid = (right + left) / 2;
            if(nums[mid] == target)
                return true;
            else if(right - left == 1)
            {
                if(nums[right] == target)
                    return true;
                break;
            }
            else if(nums[mid] > nums[left])
            {
                if(nums[mid] > target && nums[left] <= target)
                    right = mid;
                else
                    left = mid;
            }
            else if(nums[mid] < nums[right])
            {
                if(nums[mid] < target && nums[right] >= target)
                    left = mid;
                else
                    right = mid;
            }
        }
        
        return ans;
    }
};


//方法一:自测Accepted
//classified discussion
//1. Based on the property of rotated array, there may or may not have one sorted sequence 
//when one sequence is divided into two parts  
//2. make decision under all these cases
class Solution {
public:
    bool search(int A[], int n, int target) 
	{
        if( n==0 )
			return false;
        int mid = 0;
		int left=0, right=n-1;
		while( left <= right)
		{
			mid = left + (right-left)/2 ;
			if(A[mid] == target)
				return true;
			if( A[left] < A[mid] ) //left side sorted, including mid, it is sorted in [l,mid]
			{
				if( A[left] <= target && target < A[mid] )
					right = mid - 1;
				else
					left = mid + 1;
			}
			else if ( A[mid] < A[right] ) //right side sorted, [mid,r] must be sorted, if [l,mid] not sorted
			{
				if ( A[mid] < target && target <= A[right] )
					left = mid + 1;
				else
					right = mid - 1;
			}
			else if ( A[left] == A[mid] ) //A[m] is not the target, so remove en element equal to A[m] is safe
				left++;
			else if ( A[right] == A[mid] ) //ditto
				right--;
		}
		return false;
    }
};


你可能感兴趣的:(LeetCode,C++,array,search,binary)