FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
LeetCode-Solutions/C++/single-element-in-a-sorted-array.cpp at master · fileset/LeetCode-Solutions · GitHub
fileset
LeetCode-Solutions
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
LeetCode-Solutions
/
C++
/
single-element-in-a-sorted-array.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
21 lines (19 loc) · 556 Bytes
Breadcrumbs
LeetCode-Solutions
/
C++
/
single-element-in-a-sorted-array.cpp
Copy path
File metadata and controls
21 lines (19 loc) · 556 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//
Time: O(logn)
//
Space: O(1)
class
Solution
{
public:
int
singleNonDuplicate
(vector<
int
>& nums) {
int
left =
0
, right = nums.
size
() -
1
;
while
(left <= right) {
auto
mid = left + (right - left) /
2
;
if
(!(mid %
2
==
0
&& mid +
1
< nums.
size
() &&
nums[mid] == nums[mid +
1
]) &&
!(mid %
2
==
1
&& nums[mid] == nums[mid -
1
])) {
right = mid -
1
;
}
else
{
left = mid +
1
;
}
}
return
nums[left];
}
};
Back
|
FazBrowse Home
|
New Git URL