LeetCode Easy: 33. Search in Rotated Sorted Array

1. The topic

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).

You are given a target value to search. If found in the array return its index, otherwise return -1.

You may assume no duplicate exists in the array.

Your algorithm's runtime complexity must be in the order of O(log n).

Example 1:

Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4

Example 2:

Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1

The gist of the title: Given a rotation array and a target, return the subscript where the target is located.

2. Ideas

Idea 1. Because there is a point in the rotation array that destroys the order of the array, but there is only one point, it is ordered before this point, and it is also ordered after this point, so you can find this turning point first, and then Both sides can logically use the dichotomy to search. The code below uses this idea.

Idea 2. If it is a normal sorted array, then the subscript where the target is located can be quickly found using the dichotomy method, but the given may be a rotating array, and the dichotomy method cannot be used directly, but the rotating array cannot be used. Use the dichotomy directly, but some of its data can be used. If we divide the rotation array into two parts, then one part can definitely use the dichotomy method, and the remaining part is still a rotation array, which can continue to be divided into two parts, and iterative loops can be used. Or recursively. This is an idea.

3. Code

#coding:utf-8
class Solution:
    def search(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        if len(nums) <= 0:
            return -1
        pivot = self.FindPivot(nums)
        if pivot == -1:
            return self.Search(nums,0,len(nums)-1,target)
        if nums[pivot] == target:
            return pivot
        if nums[0] <= target:
            return self.Search(nums,0,pivot,target)
        else:
            return self.Search(nums,pivot+1,len(nums)-1,target)

    def Search(self,nums,start,end,target):
        if start > end:
            return -1
        while start <= end:
            mid = (start + end)//2
            if nums[mid] == target:
                print(mid)
                return mid
            elif nums[mid] > target:
                end = mid -1
            else:
                start = mid + 1
        print(-1)
        return -1

    def FindPivot(self,nums):
        start = 0;end = len(nums)-1
        if nums[end] > nums[start]: # the list sorted
            return -1
        # search rotated list
        while(start <= end):
            mid = (start + end)//2
            if nums[mid] > nums[start]:  # the Pivot is in [mid,end]
                start = mid
            elif nums[mid] < nums[start]:
                end = mid
            else:
                return mid

if __name__ == '__main__':
    ss = Solution()
    nums = [3,1]
    ss.search(nums,1)

 Reference blog: https://blog.csdn.net/sunnyyoona/article/details/18313497

Guess you like

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