"033-search in rotated Sorted array (search in rotated array)"
"leetcode-Interview algorithm classic-java Implementation" "All topics Directory 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. Returns the corresponding subscript if it exists, otherwise returns-1.
Assume that there are no duplicate values in the array.
Thinking of solving problems
First in the array to find the smallest element corresponding to the subscript, if the subscript is 0 to indicate that the entire array is ordered, if not the description of the array is divided into two ordered intervals, the judge to find the element is that an ordered interval, and then to find.
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; }}
Evaluation Results
Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.
Special Instructions
Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47064941"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Leetcode-Interview algorithm classic-java Implementation" "033-search in rotated Sorted array (search in rotated array)"