[Leetcode 33] Search in rotated Sorted Array

Source: Internet
Author: User

1 topics

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.

Hide TagsArray Binary Search2 thought for a while, considering the target and the size of the medium and two sides to determine the next two-point search for the low and high. The result is too many branches, some problems are not considered, give up. Look at other people's ideas, Https://leetcode.com/discuss/19959/solution-logn-binary-search-solution-with-explanation-java. There must be one side of the mid-order, according to this idea, it is good to do. 3 Code
    //The left or right side of mid is always lined up in a good order. //if in the order of the inside, then the simple search, otherwise it is not in the orderly side     Public intSearchint[] A,inttarget) {        intLow = 0; intHigh = A.length-1;  while(Low <High ) {            intMid = (low + high + 1)/2; if(A[mid] = = target)returnmid; if(A[mid] >A[low]) {                if(Target < A[low] | | target >A[mid]) { Low= Mid + 1; } Else{ High= Mid-1; }            } Else {               if(Target < A[mid] | | target >A[high]) { High= Mid-1; } Else{ Low= Mid+1; }            }        }        returnLow < A.length && A[low] = = target? Low:-1; }

[Leetcode 33] Search in rotated Sorted Array

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.