"153-find Minimum in rotated Sorted array (find the smallest number in the 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
).
Find the minimum element.
Assume no duplicate exists in the array.
Main Topic
Suppose that a sorted array is rotated with one of the previously unknown hubs.
That is, 0 1 2 4 5 6 7 May become 4 5 6 7 0 1 2).
Find the smallest element.
You can assume that there are no duplicates in the array.
Thinking of solving problems
Binary search method, the array is divided into two ordered parts, the previous part and the next part and the previous ordering element is larger than the next element, as long as the next element is found than the previous one is the element to be found
Code Implementation
Algorithm implementation class
Public classSolution { Public int Findmin(int[] nums) {//parameter checking if(Nums = =NULL|| Nums.length <0) {Throw NewIllegalArgumentException (); }returnBinarySearch (Nums,0, Nums.length-1); } Public int BinarySearch(int[] Nums,intStartintEnd) {intMID =0; 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]) {returnNums[mid +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 returnnums[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/47828181"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Leetcode-Interview algorithm classic-java Implementation" "153-find Minimum in rotated Sorted array (find the smallest number in the rotated array)"