"Leetcode-Interview algorithm classic-java Implementation" "033-search in rotated Sorted array (search in rotated array)"

Source: Internet
Author: User

"033-search in rotated Sorted array (search in rotated array)" "leetcode-Interview algorithm classic-java Implementation" "All Topics folder Index" Original Question

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.

Main Topic

Suppose a sorted array is rotated with an unknown pivot. (That is, 0 1 2 4 5 6 7 May become 4 5 6 7 0 1 2).
Given a target value, search in the array.

If present, the corresponding subscript is returned. otherwise returns-1.
Assume that there are no recurrence values in the array.

 

Thinking of solving problems

The corresponding subscript for the smallest element is first found in the array, assuming that the subscript is 0 to indicate that the entire array is sequential. The assumption is not that the array is divided into two ordered intervals, and that the element to be found is the ordered interval, and then it is searched.

Code Implementation

Algorithm implementation class

 Public  class solution {     Public int Search(int[] Nums,intTarget) {if(Nums! =NULL&& nums.length >0) {//Find the lowest element corresponding subscript            intMinindex = Searchminindex (Nums,0, Nums.length-1);//entire array is globally ordered            if(Minindex = =0) {returnBinarySearch (Nums,0, Nums.length-1, target); }//There are two local ordered intervals, such as 4 5 6 7 8 9 0 1 2 3            Else{//Tian Good and the last element of the next ordered interval is equal, return the corresponding subscript                if(Nums[nums.length-1] = = target) {returnNums.length-1; }//target may be in the next ordered interval                Else if(Nums[nums.length-1] > Target) {returnBinarySearch (Nums, Minindex, Nums.length-1, target); }//target may be in the previous ordered interval                Else{returnBinarySearch (Nums,0, Minindex-1, target); }            }        }return-1; }/** * Binary search * * @param nums array * @param start start position * End of @param end Location * @param target Search destination * @return The subscript of the matching element */     Public int BinarySearch(int[] Nums,intStartintEndintTarget) {intMid while(Start <= end) {mid = start + ((End-start) >>1);if(Nums[mid] = = target) {returnMid }Else if(Nums[mid] > target) {end = mid-1; }Else{start = mid +1; }        }return-1; }/** * Find the lowest element subscript * * @param nums array * @param start start position * @param End knot Beam position * @return The minimum element subscript */     Public int Searchminindex(int[] Nums,intStartintEnd) {intMid while(Start < end) {mid = start + ((End-start) >>1);//After a number is smaller than the previous number is found            if(Nums[mid] > Nums[mid +1]) {returnMid +1; }//Description intermediate value in the first ordered array            Else if(Nums[mid] > Nums[start])            {start = mid; }//Description intermediate value in second ordered array            Else{end = Mid; }        }//indicates that the entire array is ordered        return 0; }}
Assessment Results

  Click on the picture. Mouse does not release, drag a position. After release, view the full picture in the new form.

Special Instructions Welcome reprint. Reprint Please specify the source "http://blog.csdn.net/derrantcm/article/details/47064941"

"Leetcode-Interview algorithm classic-java Implementation" "033-search in rotated Sorted array (search in rotated 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.