文章目录


数据结构算法操作试题(C++/Python):数据结构算法操作试题(C++/Python)——目录


1. 题目

leetcode 链接:https://leetcode-cn.com/problems/search-in-rotated-sorted-array/

数据结构算法操作试题(C++/Python)——搜索旋转排序数组

2. 解答

python: 24ms, 11mb

class Solution(object):
    def search(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        if not nums:return -1
        left = 0 
        right = len(nums)-1
        while left<=right:
            mid = (left+right)/2
            if nums[mid]==target:
                return mid
            if nums[left] <=nums[mid]:
                if nums[left]<=target<=nums[mid]:
                    right = mid-1
                else:
                    left = mid+1
            else:
                if nums[mid]<=target<=nums[right]:
                    left = mid+1
                else:
                    right = mid-1
        return -1
 

其他方法看 leetcode 链接 评论区~

相关文章:

  • 2021-06-16
  • 2021-07-30
  • 2021-07-28
  • 2021-06-09
  • 2021-12-29
  • 2021-10-18
猜你喜欢
  • 2021-12-14
  • 2021-05-12
  • 2022-12-23
  • 2021-08-08
  • 2021-09-10
相关资源
相似解决方案