# Sword refers to the offer sixth "minimum number of rotating arrays" C + + implementation

Source: Internet
Author: User
Rotate array Minimum number of participants: 3258 time limit: 1 seconds space limit: 32768K pass ratio: 14.1% best record: 0 ms|0k (from Wocao) Topic Description

Moving a number of the first elements of an array to the end of the array, we call it the rotation of the array. Enter a rotation for a non-descending sequence, 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.

This is also an orderly series, a look should be and two points, the fact is the case.

The topic gives a non-zero sequence, which means there may be duplicate numbers, to be noted.

To briefly analyze the possibility of arrays.

Rotated, not rotated, all elements are equal.

Look at the problem. The rotated array is divided into two halves, {3,4,5,} {1,2}.

You can see that if you rotate through the previous array, all elements must be greater than all of the following elements.

If not rotated, the first element must be smaller than the tail element.

If the middle element is greater than the tail element, the minimum value must be in the second half.

If the intermediate element is less than the first element, the minimum value must be in the first half.

Finally, there must be two elements left, followed by the minimum value, if the front is the minimum value is not rotating.

```int Minnumberinrotatearray (vector<int> rotatearray) {//should not be free, because it is a problem in the cattle net, there is a set of test data is empty if (Rotatearray.empty ())
{return 0;
int left=0;
int Right=rotatearray.size ()-1;
if (Rotatearray[left]<rotatearray[right]) {return rotatearray[left];
while (right>left) {int mid= (right+left)/2;
int Middle=rotatearray[mid];
int Leftnumber=rotatearray[left];
int Rightnumber=rotatearray[right];
if ((right-left) ==1) {return rightnumber;
The values in the///left and right are all equal and cannot be determined either before or after the first one is a smaller than the previous one is the minimum if (middle==leftnumber&&middle==rightnumber) { for (int i=left;i<right;i++) {if (rotatearray[i]>rotatearray[i+1]) {return rota
TEARRAY[I+1];
}//All equal words directly return the leftmost value on the line returns Leftnumber;
}else if (middle<leftnumber) {right=mid;
}else if (middle>rightnumber) {      Left=mid; }
}
}```

Related Keywords:

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.