[Leetcode]25. Search in rotated array rotation arrays Lookup

Source: Internet
Author: User

Suppose a sorted array is rotated on some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2 ).

You is given a target value to search. If found in the array is return its index, otherwise return-1.

Assume no duplicate exists in the array.

Solution 1: Sequential lookup, Time complexity O (n).

class Solution {public:    int search (vector<intint  Target) {        int0;          while (I < n && target! = Nums[i])            i+ +;         return i = = n? -1  : i;    };

Solution 2: The rotated array is divided into two ordered arrays, so you can use a binary lookup. If the first element of the array is less than the end of the array, the array is not rotated and the binarysearch can be found directly using two points; otherwise (1) initialize left=0,right=n-1, take mid= (left+right)/2, (2) if target==nums[ Mid], the direct return to mid, otherwise if NUMS[LEFT]<NUMS[MID], the description nums[left, ..., Mid-1] is ordered, and nums[mid+1, ..., n-1] is rotated, the former is called BinarySearch , if not found to the latter call search, if NUMS[LEFT]>NUMS[MID], the description nums[left, ..., mid-1] is rotating, and nums[mid+1, ..., n-1] is orderly, Call BinarySearch on the latter, and then call search on the former if not found.

classSolution { Public:    intSearch (vector<int>& Nums,inttarget) {        intn =nums.size (); if(N <2)            returnn = =1? (target = = nums[0] ?0: -1) : -1; intRes, left =0, right = N-1; if(Nums[left] <Nums[right]) Res=BinarySearch (Nums, left, right, target); Else        {            intMid = (left + right) >>1; if(target = =Nums[mid]) Res=mid; Else if(Nums[left] <Nums[mid]) {                if(res = BinarySearch (Nums, left, mid-1, target)) = =-1) {vector<int> tmp (Nums.begin () + Mid +1, Nums.end ()); Res=Search (tmp, target); Res= Res = =-1? -1: Res + mid +1; }            }            Else            {                if(res = BinarySearch (nums, Mid +1, right, target) = =-1) {vector<int> tmp (Nums.begin (), Nums.begin () +mid); Res=Search (tmp, target); }            }        }        returnRes; }Private:    intBinarySearch (vector<int>& Nums,intLeftintRightintkey) {        if(Left >Right )return-1; intMid = (left + right) >>1; if(Key = =Nums[mid])returnmid; Else if(Key >Nums[mid])returnBinarySearch (Nums, Mid +1, right, key); Else            returnBinarySearch (Nums, left, mid-1, key); }};

The above code can be further optimized. Comparison with Target and Nums[left], Nums[right], Nums[mid] can remove some of the next unnecessary lookups. For Nums[left]>nums[mid], if Target>nums[mid]&&target>nums[right], the next step is just nums[left,..., mid-1] If Target>nums[mid]&&target<=nums[right], the next step is to call BinarySearch lookup in nums[mid+1,..., right] , if Target<nums[mid], the next step is to call search lookup in Nums[left,..., mid-1] only. For Nums[left]<nums[mid], if Target<nums[mid]&&target<nums[left], just nums[mid+1,..., right] If Target<nums[mid]&&target>=nums[left], you only need to call BinarySearch find in the Nums[left,..., mid-1] If Target>nums[mid], you only need to call search lookup in nums[mid+1,..., right].

[leetcode]25. Search in rotated array to rotate arrays find

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.