Leetcode_33 rotation sorted array [search]

Article Directory:

  • topic
  • A script
  • A script logic

topic:

Suppose ascending order according to the array was rotated in a previously unknown point.

(E.g., array [0,1,2,4,5,6,7] may become [4,5,6,7,0,1,2]).

Searching for a given target value, if the existence of the target array, its index returns, otherwise it returns -1.

You can assume that the array element does not exist in duplicate.

Your time complexity of the algorithm must be O (log n) level.

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


A script: [when using: 44ms]

class Solution:
    def search(self, nums: List[int], target: int) -> int:
        if target in nums:
            return(nums.index(target))
        else:
            return(-1)

A script logic:

  • This question is too simple, I will not elaborate here

Guess you like

Origin www.cnblogs.com/mailong/p/12057596.html