标签:就会 blog 旋转数组 div end logs cto 最小数 solution
一种错误写法:
class Solution { public: int minNumberInRotateArray(vector<int> rotateArray) { int length = rotateArray.size(); if(length <= 0) return 0; if(rotateArray[0] == rotateArray[length-1] && rotateArray[0] == rotateArray[(length-1)/2]){ int number = 0x7FFFFFFF; for(int i = 0;i < length;i++){ if(rotateArray[i] < number) number = rotateArray[i]; } return number; } else if(rotateArray[0] < rotateArray[length-1]){ return rotateArray[0]; } else{ int begin = 0; int end = length-1; while(begin < end){ int mid = (begin + end)/2; if(rotateArray[mid] >= rotateArray[begin]) begin = mid; else if(rotateArray[mid] <= rotateArray[begin]) end = mid; } return rotateArray[begin]; } } };
如果只有两个数,begin < end就会陷入死循环
标签:就会 blog 旋转数组 div end logs cto 最小数 solution
原文地址:http://www.cnblogs.com/ymjyqsx/p/7514740.html