-
Notifications
You must be signed in to change notification settings - Fork 0
LC 0540 [M] Single Element in a Sorted Array
Code with Senpai edited this page Oct 30, 2020
·
2 revisions
first binary search with modified m and l
class Solution:
def singleNonDuplicate(self, nums: List[int]) -> int:
l, r = 0, len(nums) - 1
while l < r:
m = (l + r) // 2
if m % 2 == 1:
m -= 1 # should be at even index
if nums[m] == nums[m+1]:
l = m + 2 # yes pair, single must be on right, +1 to skip dupe
else:
r = m # no pair, single must be on left
return nums[l]
footer