题目描述:
python实现 Search in Rotated Sorted Array2 搜索旋转排序数组
中文:
假设按照升序排序的数组在预先未知的某个点上进行了旋转。
( 例如,数组 [0,0,1,2,2,5,6] 可能变为 [2,5,6,0,0,1,2] )。
编写一个函数来判断给定的目标值是否存在于数组中。若存在返回 true,否则返回 false。
英文:
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
题目来源:力扣题库