[Leetcode] (python): 033-search in rotated Sorted Array

Source: Internet
Author: User

Source of the topic:


Test Instructions Analysis:

Implements a lookup in a flipped array. (What is called the inverted array, that is, the original sequence of the array, select a point, the number of points before the point is placed behind the array, as 4,5,6,7,1,2,3 is a flip array).

Topic Ideas:

The idea of this topic is similar to that of binary search, and the difficulty is mainly in determining the position of two points.

Code (Python):


1 classsolution (object):2     defSearch (self, nums, target):3         """4 : Type Nums:list[int]5 : Type Target:int6 : Rtype:int7         """8Size =Len (nums)9First = 0;last =sizeTen          whileFirst! =Last : OneMid = (first + last)//2 A             ifNums[mid] = =Target: -                 returnMid -             ifNums[first] <=Nums[mid]: the                 ifNums[first] <= Target andTarget <Nums[mid]: -Last =Mid -                 Else: -First = mid + 1 +             Else: -                 ifNums[mid] < target andTarget <= nums[last-1]: +First = mid + 1 A                 Else: atLast =Mid -         return-1
View Code

Reprint Please specify source: http://www.cnblogs.com/chruny/p/4918379.html

[Leetcode] (python): 033-search in rotated Sorted Array

Related Article

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.