"Sword means offer" six, rotate the smallest number in the array

Source: Internet
Author: User

Title Descriptionmoves the first element of an array to the end of the array, which we call the rotation of the array. Enter a rotation of an incrementally sorted array, outputting the smallest element of the rotated array. For example, the array {3,4,5,1,2} is a rotation of {1,2,3,4,5}, and the minimum value of the array is 1. Analysis: When an array is rotated, it becomes a locally ordered array, divided into two parts, which are incremented. To find the smallest element, we first think of the bubble sort once, or a trip to select the sort. Or just find a[i]<a[i-1] is the point that you want to find. You can also use binary to find the side where the minimum value is located.
1 Importjava.util.ArrayList;2  Public classSolution {3      Public intMinnumberinrotatearray (int[] Array) {4         if(array==NULL|| Array.length==0){5             return0 ;6         }7         if(array.length==1| | Array[0]<array[array.length-1]){8             returnArray[0] ;9         }Ten         if(array.length==2){ One             returnMath.min (Array[0], array[1]) ; A         } -         intMin = array[0] ; -          for(inti = 1; I <array.length; i++  ){ the             if(array[i]<min) { -Min =Array[i]; -             } -         } +         returnmin; -     } +}

The second method of

Importjava.util.ArrayList; Public classSolution { Public intMinnumberinrotatearray (int[] Array) {        if(Array.Length = = 0)return0;///not A[0]         for(inti = 1; i < Array.Length; ++i) {if(Array[i] < array[i-1])returnArray[i]; }        returnArray[0]; }}

"Sword means offer" six, rotate the smallest number in the 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.