class Solution { public: int minNumberInRotateArray(vector<int> rotateArray) { if (rotateArray.empty()) return 0; int front = 0; int back = rotateArray.size() - 1; int mid = (front + back) / 2; while (rotateArray[front] >= rotateArray[back]){ if (rotateArray[mid] >= rotateArray[front]){ front = mid +1; mid = (front + back) / 2; } else if (rotateArray[mid] <= rotateArray[back]){ back = mid; mid = (front + back) / 2; } if (front == back) break; } return rotateArray[front]; } };
非递减数列旋转后是两段非递减数列,采用二分查找。以下罗列注意点
1、存在一开始头=尾的情况,故while条件中有=
2、二分查找时,我们在意的是min在前半区还是后半区:
(1)mid >= front,min在后边
(2)mid <= back, min在前边
最后的情况是mid=front=back,所以要强制退出循环,bug调的焦头烂额。
欢迎交流指正!!!