Skip to the content.

leetcode [33] 搜索旋转排序数组


Contact me:

Blog -> https://cugtyt.github.io/blog/index
Email -> cugtyt@qq.com
GitHub -> Cugtyt@GitHub


假设按照升序排序的数组在预先未知的某个点上进行了旋转。

( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。

搜索一个给定的目标值,如果数组中存在这个目标值,则返回它的索引,否则返回 -1 。

你可以假设数组中不存在重复的元素。

你的算法时间复杂度必须是 O(log n) 级别。

示例 1:

输入: nums = [4,5,6,7,0,1,2], target = 0
输出: 4

示例 2:

输入: nums = [4,5,6,7,0,1,2], target = 3
输出: -1

解法1:

思路:

  1. 对于搜索区间小于3的情况,直接线性查找

  2. 二分:

    分三种情况:

    • 上坡, 那么可以分为第一个坡,和第二个坡,如果当前坡在目标区间,那么正常二分,否则,逼近目标区间
    • 尖峰, 如果尖峰小于目标,无解。否则,判断目标区间在哪个坡,逼近目标区间
    • 凹陷, 如果凹陷大于目标,无解,否则,判断目标区在哪个,逼近目标区间。
class Solution:
    # def search(self, nums: List[int], target: int) -> int:
    def search(self, nums, target: int) -> int:
        if len(nums) < 3 or nums[-1] == nums[0]:
            if target in nums:
                return nums.index(target)
            else:
                return -1
        if nums[0] == target:
            return 0
        if nums[-1] == target:
            return len(nums) - 1

        first = target > nums[0]
        second = not first
        low = 0
        high = len(nums)
        while low < high:
            mid = (low + high) // 2
            if nums[mid] == target:
                return mid
            if high - low < 3:
                if target in nums[low: high]:
                    return nums.index(target, low, high)
                else:
                    return -1
            # ^^^
            if nums[mid - 1] < nums[mid] < nums[mid + 1]:
                # same domain
                if (nums[0] < nums[mid] and first
                ) or (nums[0] > nums[mid] and second):
                    if nums[mid] < target:
                        low = mid + 1
                    else:
                        high = mid
                # diff domain
                elif nums[0] < nums[mid] and second:
                    low = mid + 1
                else:# nums[0] > nums[mid] and first:
                    high = mid
            # ^^-
            elif nums[mid - 1] < nums[mid] > nums[mid + 1]:
                if nums[mid] < target:
                    return -1
                if nums[0] > target:
                    low = mid + 1
                else:
                    high = mid
            # ^-^
            else:
                if nums[mid] > target:
                    return -1
                if nums[0] < target:
                    high = mid
                else:
                    low = mid + 1

        return -1

解法2:简化

class Solution:
    # def search(self, nums: List[int], target: int) -> int:
    def search(self, nums, target: int) -> int:
        if len(nums) < 5 or nums[-1] == nums[0]:
            if target in nums:
                return nums.index(target)
            else:
                return -1
        if nums[0] == target:
            return 0
        if nums[-1] == target:
            return len(nums) - 1

        first = target > nums[0]
        low = 0
        high = len(nums)
        while low < high:
            mid = (low + high) // 2
            if nums[mid] == target:
                return mid
            if high - low < 5:
                if target in nums[low: high]:
                    return nums.index(target, low, high)
                else:
                    return -1
            # 当前是否在第一个坡
            current = nums[0] < nums[mid]
            # 如果区间匹配
            if current == first:
                if nums[mid] < target:
                    low = mid + 1
                else:
                    high = mid
            # 不匹配
            elif first and not current:
                high = mid
            else:
                low = mid + 1

        return -1

解法3:

思路: 二分查找,对于mid值进行比较,每次比较要考虑是在左坡上还是右坡上,必要的时候再对left值进行判断。注意要对left和right仅差1的情况处理,因为这种情况下会出现找不到的情况,为了能跳出,强制left右移。

class Solution:
    def search(self, nums: List[int], target: int) -> int:
        if len(nums) == 0: return -1
        
        left, right = 0, len(nums)
        while left < right:
            mid = (left + right) // 2
            if nums[mid] < target:
                # 落在左坡上
                if nums[left] < nums[mid]: left = mid + 1
                # 落在右坡上
                elif nums[left] > nums[mid]: 
                    if nums[left] < target: right = mid
                    elif nums[left] > target: left = mid + 1
                    else: return left
            elif nums[mid] > target:
                # 落在左坡上
                if nums[left] < nums[mid]: 
                    if nums[left] < target: right = mid
                    elif nums[left] > target: left = mid + 1
                    else: return left
                # 落在右坡上
                elif nums[left] > nums[mid]: right = mid
            else:
                return mid

            if left == mid: left += 1
        return -1