LeetCode (power button) - Search in Rotated Sorted Array2 search rotation sorted array python achieve

Subject description:

python achieve  Search in Rotated Sorted Array2 search rotation sorted array  

  Chinese:

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

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

  Write a function to determine whether a given target is present in the array. Returns true if there is, otherwise false.

  English:

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

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

  You are given a target value to search. If found in the array return true, otherwise return false.

class Solution(object):
    def search(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: bool
        """
        start=0
        
        ends=len(nums)-1
        while start<=ends:
            mid=(start+ends)/2
            if nums[mid]==target: return True
            if nums[start]==nums[mid]==nums[ends]:
                start+=1;
                
                ends-=1
            elif nums[start]<=nums[mid]:
                if nums[start]<=target<nums[mid]:
                    
                    ends=mid-1
                else: start=mid+1
            else:
                if nums[mid]<=target<nums[start]: 
                    
                    start=mid+1
                else:
                    
                    ends=mid-1
        return False 

 

 

Topic Source: stay button Exam



Guess you like

Origin www.cnblogs.com/spp666/p/11536451.html