一种错误写法:
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就会陷入死循环
时间: 2024-10-11 00:49:22