题解 | #旋转数组的最小数字#
旋转数组的最小数字
https://www.nowcoder.com/practice/9f3231a991af4f55b95579b44b7a01ba
class Solution { public: int minNumberInRotateArray(vector<int> rotateArray) { //如果他的最右边小于中间元素,那么最小值一定在右边; //如果是大于,因为他一开始是有序的,所以一定会在mid左边,包括mid //相等的话只能一次次缩短右边 int left = 0; int right = rotateArray.size()-1; while(left<right) { int mid = (right+left)/2; if(rotateArray[mid] > rotateArray[right]) left = mid+1; else if(rotateArray[mid] < rotateArray[right]) right = mid; else if(rotateArray[mid] == rotateArray[right]) --right; } return rotateArray[left]; } };