-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
29 lines (27 loc) · 873 Bytes
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public:
int singleNonDuplicate(vector<int>& nums) {
int l = 0, r = nums.size() - 1;
while (l < r) {
int m = l + (r - l) / 2;
if (m % 2 == 0) {
if (nums[m - 1] == nums[m]) {
r = m - 2;
} else if (nums[m] == nums[m + 1]) {
l = m + 2;
} else { // nums[m - 1] != nums[m] && nums[m] != nums[m + 1]
return nums[m];
}
} else {
if (nums[m - 1] == nums[m]) {
l = m + 1;
} else if (nums[m] == nums[m + 1]) {
r = m - 1;
} else { // nums[m - 1] != nums[m] && nums[m] != nums[m + 1]
return nums[m];
}
}
}
return nums[l];
}
};