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

Source: Internet
Author: User

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

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.