Leetcode "Search in rotated Sorted Array II" Python implementation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.