title :
Almost the same as the previous one; the difference is that duplicate elements are allowed in the array, but the topic requirements are simple, just return TRUE or false
Http://www.cnblogs.com/xbf9xbf/p/4254590.html
code : OJ Test via runtime:73 ms
1 classSolution:2 #@param a a list of integers3 #@param target an integer4 #@return A Boolean5 defSearch (self, A, target):6A=list (set (A))7 #None Case & Zero case8 ifA isNoneorLen (A) = =0:9 returnFalseTen #binary Search OneStart =0 Aend = Len (A)-1 - whilestart<=End: - #One element left case the ifStart = =End: - ifa[start]==Target: - returnTrue - Else: + returnFalse - #Elements left case + ifStart+1 = =End: A ifa[start]==Target: at returnTrue - elifa[end]==Target: - returnTrue - Else: - returnFalse - #equal or more than three elements case inMid = (start+end)/2 - ifa[mid]==Target: to returnTrue + elifA[mid]>Target: - ifA[start]>a[mid] anda[end]<A[mid]: theStart = Mid+1 * elifA[start]<a[mid] anda[end]<A[mid]: $ ifa[end]>=Target:Panax NotoginsengStart = Mid+1 - Else: theEnd = Mid-1 + elifA[start]>a[mid] andA[end]>A[mid]: AEnd = Mid-1 the Else: +End = Mid-1 - Else: $ ifA[start]>a[mid] anda[end]<A[mid]: $End = Mid-1 - elifA[start]<a[mid] anda[end]<A[mid]: -Start = Mid+1 the elifA[start]>a[mid] andA[end]>A[mid]: - ifa[end]>=Target:WuyiStart = Mid+1 the Else: -End = Mid-1 Wu Else: -Start = Mid+1 About returnFalse
Ideas :
Used a trick Python array to a=list (set (A))
So that there is no duplicate element in the a array, you can directly use the previous question of the code.
Such trick should not be the original intention of the topic, this two-point search for more classical topics, should be thoroughly understand.
Leetcode "Search in rotated Sorted Array II" Python implementation