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