力扣540.有序数组中的单一元素
-
找到 偶数位做二分
- 若不等 说明当前mid在单一元素右侧 r = mid
- 若相等 说明当前mid在单一元素左侧 l = mid + 2
-
class Solution { public: int singleNonDuplicate(vector<int>& nums) { int l = 0,r = nums.size() -1; while(l<r) { int mid = l + r >> 1; if(mid % 2 == 1) mid --; if(nums[mid] == nums[mid+1]) l = mid+2; else r = mid; } return nums[l]; } };