An ordered array, without repeating numbers, pushing right several times to find the minimum value. For example
(i.e., 0 1 2 4 5 6 7
might become 4 5 6 7 0 1 2
).
It doesn't make sense to go straight through once to find the answer. I've done a similar problem before, but I can't find a specific number. So just search the next garnker, know is search in rotated Sorted Array, the problem is to find the target value. Here is the minimum value to find.
I think so, divided into several parts,
1. If the value on the left is equal to the middle value, then the remaining two numbers are to be judged, and are adjacent.
2. If the middle value is greater than the value on the left, then the minimum value is definitely not on the leftmost side but only on the right.
3. If the median value is less than the value on the left, then the minimum value is not the middle value or the left piece.
classSolution { Public: intFindmin (vector<int> &num) { intLen = Num.size (), left =0, right = Len-1, Mid = (left + right)/2, Minmum =Int_max; while(Left! =Right ) {Mid= (left + right)/2; if(Num[mid] = =Num[left]) { if(Num[left] >Num[right]) left=Right ; Break; } Else if(Num[mid] >Num[left]) { if(Num[left] < minmum) Minmum =Num[left]; Left= Mid +1; } Else if(Num[mid] <Num[left]) { if(Num[mid] < minmum) Minmum =Num[mid]; Right= Mid-1; }} minmum=min (minmum, num[left]);//This can't be forgotten.returnMinmum; }};
Leetcode Find Minimum in rotated Sorted Array