题解 | #旋转数组的最小数字#
旋转数组的最小数字
http://www.nowcoder.com/practice/9f3231a991af4f55b95579b44b7a01ba
class Solution { public: int minNumberInRotateArray(vector<int> rotateArray) { int arrlen = rotateArray.size(); if (arrlen < 3) { return min(rotateArray[0], rotateArray[1]); } if (rotateArray[arrlen - 1] > rotateArray[0]) { return rotateArray[0]; } int m = int(arrlen / 2) + 1; vector<int> lv; lv.assign(rotateArray.begin(), rotateArray.end() - m+1); int l = minNumberInRotateArray(lv); vector<int> rv; rv.assign(rotateArray.begin() + m-1, rotateArray.end()); int r = minNumberInRotateArray(rv); return min(l, r); } };
简单的二分法处理,关键在于中间值m的控制