美文网首页
leetcode33. 搜索旋转排序数组

leetcode33. 搜索旋转排序数组

作者: 冰源 | 来源:发表于2018-10-10 22:02 被阅读4次
搜索旋转排序数组
#python3
class Solution:
    def search(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        left = 0
        right =  len(nums)-1

        # 先找到最小数的位置
        # 左大右小
        while left<right:
            mid = (left+right)//2
            if nums[mid]>nums[right]: left=mid+1
            else:right=mid
        root = right  # 此时right = left

        left = 0
        right =  len(nums)-1
        while left<=right:
            mid = (left+right)//2
            realmid = (mid+root)%len(nums)
            if nums[realmid]==target:return realmid
            if nums[realmid]<target:left=mid+1
            else:right=mid-1
        return -1

相关文章

网友评论

      本文标题:leetcode33. 搜索旋转排序数组

      本文链接:https://www.haomeiwen.com/subject/aromaftx.html