33. Search in Rotated Sorted Array

33Search in Rotated Sorted Array

Topic description

Find the target value target in the rotated sorted array

analyze

Method 1: In
special cases, if the array is empty, return -1
while loop to find the target, dichotomy:
(check which range of the target is in the current low, mid, and high, and update the boundary)
1. If both low and mid are in the unrotated area
1. Determine whether the target is between low-mid, if so, update the right boundary high;
2. Otherwise, update the left boundary low
2. Other situations:
1. Determine whether the target is between mid-high, if so, update the left boundary low;
2 . else, update the right border high

If there is no previous return, that is, not found, return -1

full code

class Solution:
    def search(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        if not nums: #Special case: the array is empty
            return -1

        low, high = 0, len(nums) - 1

        while low <= high: #loop condition
            mid = (low + high) // 2
            if target == nums[mid]: #find the target
                return mid
            #low and mid are both in the unrotated area
            if nums[low] <= nums[mid]:  
                #If target is between low and mid, update the right endpoint
                if nums[low] <= target <= nums[mid]:
                    high = mid - 1
                #If target is not between low and mid, update the left endpoint  
                else:
                    low = mid + 1
            #If nums[low] > nums[mid], that is, low is in the unrotated area, and mid is in the rotated area
            else:
                #If target is between mid and high, update the left endpoint
                if nums[mid] <= target <= nums[high]:
                    low = mid + 1
                else:
                    high = mid - 1

        return -1 #If no result was returned before, if not found, return -1

analyze

Method Two:

1. Use the dichotomy method to find the index MinIdx of the minimum value in the nums array:
when nums[mid]>nums[right], update left=mid+1; in other cases, right=mid
2. Compare target and the last element of nums The size relationship defines the starting and ending positions of the array to be searched:
target>nums end element: target is located in the first half of the array (unrotated area), start=0, end=MinIdx-1
target<nums end element: target is located in the second half of the array part (rotation area), start=MinIdx, end=end of array

3. Use dichotomy to search for target in nums[start:end]

full code

public int search(int[] nums, int target) {
    int minIdx = findMinIdx(nums);
    if (target == nums[minIdx]) return minIdx;
    int m = nums.length;
    int start = (target <= nums[m - 1]) ? minIdx : 0;
    int end = (target > nums[m - 1]) ? minIdx : m - 1;
    
    while (start <= end) {
        int mid = start + (end - start) / 2;
        if (nums[mid] == target) return mid;
        else if (target > nums[mid]) start = mid + 1;
        else end = mid - 1;
    }
    return -1;
}

public int findMinIdx(int[] nums) {
    int start = 0, end = nums.length - 1;
    while (start < end) {
        int mid = start + (end -  start) / 2;
        if (nums[mid] > nums[end]) start = mid + 1;
        else end = mid;
    }
	return start;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324618377&siteId=291194637