Today, it is very interesting to meet such a topic, to write down!
title : Move a number of elements at the beginning of an array to the end of the array, which we call the rotation of the array. Enter a rotation of a non-descending 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. Note: All elements given are greater than 0, and if the array size is 0, return 0.
When encountering this problem, the first reaction is a very common solution, which traverses the entire array to find the smallest number. The code is as follows:
int Minnumberinrotatearray (vector<int> rotatearray)
{
int tem=0;
int len=rotatearray.size ();
if (len==0) return 0;
TEM=ROTATEARRAY[0];
for (int i=1;i<len;i++)
{
if (Tem>rotatearray[i]) tem=rotatearray[i];
}
return tem;
}
Later on the web found a more concise code, as follows:
int MinNumberInRotateArray2 (vector<int> rotatearray)
{
int len=rotatearray.size ();
Sort (Rotatearray.begin (), Rotatearray.end ());
return rotatearray[0];
}
(The above concise version of Code Source: https://www.nowcoder.com/profile/648947/codeBookDetail?submissionId=2954831)
C + + to find the smallest number of rotated arrays