"Leetcode-Interview algorithm classic-java Implementation" "153-find Minimum in rotated Sorted array (find the smallest number in the rotated array)"

Source: Internet
Author: User

"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)"

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.