540. 有序数组中的单一元素 - 力扣(LeetCode)
二分查找
使用条件 : 有序 , log n
class Solution {
public:int singleNonDuplicate(vector<int>& nums) {int left = 0, right = nums.size() - 1, mid;while (left < right){mid = ( left + right ) / 2;if (mid % 2 == 1){mid -= 1;}if ( nums[mid] == nums[mid + 1] ){ //速说明这个数在右边left = mid + 2;}else {right = mid;}}return nums[left];}
};