0540.cpp (426B)
1 class Solution { 2 public: 3 int singleNonDuplicate(vector<int> &nums) { 4 int low = 0, high = nums.size() - 1; 5 while (low < high) { 6 int mid = low + (high - low) / 2; 7 if ((mid % 2 == 0 && nums[mid] == nums[mid + 1]) || (mid % 2 == 1 && nums[mid] == nums[mid - 1])) 8 low = mid + 1; 9 else 10 high = mid; 11 } 12 return nums[low]; 13 } 14 };