Source of the topic
https://leetcode.com/problems/search-in-rotated-sorted-array-ii/
Follow to "Search in rotated Sorted Array":
What if duplicates is allowed?
Would this affect the run-time complexity? How and why?
Write a function to determine if a given the target was in the array.
Test instructions Analysis
Input:
: Type Nums:list[int]
: Type Target:int
Output:
Rtype:bool
Conditions: An ordered array of flips, array elements may be repeated to determine whether the target is in the array
Topic ideas
The key is to distinguish the boundary, using first for the lower bound, last for the upper bound, mid as the middle point. Returns true if mid is target, otherwise, determines whether the mid,first,last is equal, if equal, narrows the search space, and then determines which interval the target is in, judging by: 1) the size of target and Nums[mid], Target and Nums The size of [first].
AC Code (PYTHON)
1 classsolution (object):2 defSearch (self, nums, target):3 """4 : Type Nums:list[int]5 : Type Target:int6 : Rtype:bool7 """8Size =Len (nums)9First =0TenLast = size-1 One whileFirst <=Last : AMid = (last + first)/2 - Print(First,mid, last) - ifNums[mid] = =Target: the returnTrue - ifNums[mid] = = Nums[first] = =Nums[last]: -First + = 1; Last-= 1 - elifNums[first] <=Nums[mid]: + ifTarget < Nums[mid] andTarget >=Nums[first]: -Last = Mid-1 + Else: AFirst = mid + 1 at Else: - ifTarget >= Nums[mid] andTarget <Nums[first]: -First = mid + 1 - Else: -Last = Mid-1 - in - to returnFalse +
[Leetcode] (python): 081-search in rotated Sorted Array II