leetcode rotated sorted array

leeetcode 33. Search in Rotated Sorted Array

leetcode 81 

leetcode 153. Find Minimum in Rotated Sorted Array

This is a few similarities: are partially ordered arrays, binary search may be done. The most important is to determine in the end zone is left or right range.

leetcode 33:

IF (the nums [MID] == target) return MID; 
the else IF (the nums [MID] <target) 
{ 
    // If left ascending interval, the interval must not possible in the left half, the left half of the interval if the non-ascending order ( nums [l]> nums [mid ]), and the 
    // target> = nums [l] , it must be in the left half of the interval 
    if nums (nums [l]> nums [mid] && target> = [l]) r = . 1 mid-; 
    the else. 1 + L = MID;     
} 
the else 
{ 
    // If the right ascending section, certainly not the right half section, if non-right ascending section (nums [mid]> nums [ r]), and 
    // target <= nums [r] , it must be in the right half section 
    IF (the nums [MID]> the nums [R & lt] && target <= the nums [R & lt]) + L = MID. 1; 
    the else R & lt MID = -. 1; 
}

 

leetcode 81: 81 compared to a lot of complex 33, 33 no problem nums [mid] == nums [r] || nums [l] == nums [mid] this case, it can be used directly by comparing the size of a segment Analyzing in the end is ascending or descending order. 81 and the title nums [l] == nums [mid] || nums [mid] == nums [r] interval may be in ascending or non-ascending order.

nums [mid] <target the case, the left section, for example, if it is ordered, left a certain section possible, and if the non-sequential target> = nums [l], it must be the left interval. The question is how the case determination interval is not left the non-ordered, 33 questions by nums [l]> nums [mid] can determine a certain interval left non-ordered, but nums [l] == nums [mid] of the left section may be non-ordered, it may be ordered. If the left section of the non-ordered, nums [mid] == nums [r], if the left section is ordered, nums [mid] It is also possible == nums [r], so when nums [l] == nums [ mid] == when nums [r], the left section is determined not ordered or non-ordered, but this time based l, r necessarily impossible, so when nums [l] == nums [mid] = = nums [r], l ++, r--; 

所以当nums[mid] != target && nums[l] == nums[mid] && nums[mid] == nums[r] l++ , r-- ;

When nums [mid] <target, if nums [l] == nums [mid] Left interval must be ordered; reason is that if the left section of non-ordered, then nums [mid] == nums [r], but nums [mid] can not possibly == nums [r], it must be left interval sorted ranges. So if nums [l]> nums [mid], a left section with a constant non-sorted ranges.

if(nums[l] > nums[mid] && target >= nums[l]) r = mid - 1 ;

else l = mid  + 1 ;

nums [mid]> target when clicking on the right image;

class Solution {
public:
    bool search(vector<int>& nums, int target) 
    {
        if(nums.empty()) return false ;
        
        int l = 0 , r = nums.size() - 1 ;
        while(l <= r)
        {
            int mid = l + (r - l) / 2 ;
            
            if(nums[mid] == target) return true ;
            else if(nums[l] == nums[mid] && nums[mid] == nums[r])
            {
                l++ ;
                r-- ;
            }
            else if(nums[mid] < target)
            {
                if(nums[l] > nums[mid] && target >= nums[l]) r = mid - 1 ;
                else l = mid + 1 ;
            }
            else
            {
                if(nums[mid] > nums[r] && target <= nums[r]) l = mid + 1 ;
                else r = mid - 1 ;
            }
        }
        return false ;
    }
};

  

Expand on the issue: If you are seeking the smallest index target it? Very simple, with res record what nums [mid] all mid == target of the smallest on the list;

Guess you like

Origin www.cnblogs.com/mychen06/p/12630246.html