思路:由复杂度想到二分,对于每次二分,要考虑其奇偶性,即若是在其前方没有单独的数,则mid为偶数时应和后方数相同,奇数时和前方数相同,若是单独的数的坐标小于mid,则不同,再特判边界即可
class Solution {
public:
int singleNonDuplicate(vector<int>& nums) {
int l = nums.size();
int s = 0;
int e;
e = l - 1;
int mid;
while(s <= e){
mid = (s + e) / 2;
if(mid == 0 || mid == l - 1 || s == e){
return nums[mid];
}
if(e == s + 1){
if(nums[s] == nums[s-1]){
return nums[e];
}
return nums[s];
}
if(mid % 2 == 0){
if(nums[mid] == nums[mid+1]){
s = mid;
}else{
e = mid;
}
}else{
if(nums[mid] == nums[mid-1]){
s = mid;
}else{
e = mid;
}
}
// cout << s << " " << e << " " << mid<< " " << endl;
}
return nums[mid];
}
};