/*这个题目如果要求不重复元素二分就非常简单 含有重复元素就需要处理一些特殊的数据类型 例如[1,1,1,1] [1, 3,5]这些特判很简单 [10,10,10,10,10,1,10] [10,1,10,10,10,10,10] 这种nums[mid] = nums[right]的情况挖坑数据就比较多 需要更进一步的分类 nums[mid] nums[mid-1] nums[mid+1]的区分情况 最差情况是nums[mid] = nums[right] = nums[mid-1] = nums[mid] + 1 这种处理情况选择粗暴线性扫[left,right]维护一个最小值*/ class Solution { public: int minArray(vector<int>& numbers) { int len = numbers.size(); int left , right , mid; left = 0;right = len - 1; if(len == 2) return min(numbers[left] , numbers[right]); while(left < right){ if(numbers[left] < numbers[right]) return numbers[left]; mid = (left + right)/2; if(numbers[mid] > numbers[right]) left = mid + 1; else if(numbers[mid] < numbers[right]) right = mid; else{ int Min = numbers[left]; if(numbers[mid] == numbers[mid + 1] && numbers[mid] == numbers[mid - 1]) { for(int i = left;i <= right;i++) Min = min(numbers[i] , Min); return Min; } else if(numbers[mid] == numbers[mid - 1] && numbers[mid] != numbers[mid + 1]) left = mid + 1; else if(numbers[mid] == numbers[mid + 1] && numbers[mid] != numbers[mid - 1]) right = mid; } } return numbers[left]; } };