-
Notifications
You must be signed in to change notification settings - Fork 43
/
single-element-in-a-sorted-array.py
63 lines (59 loc) · 1.67 KB
/
single-element-in-a-sorted-array.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# V0
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/79275636
class Solution(object):
def singleNonDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
return reduce(lambda x, y: x^y, nums)
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/79275636
class Solution(object):
def singleNonDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
for i in range(0, len(nums) - 1, 2):
if nums[i] != nums[i + 1]:
return nums[i]
return nums[-1]
# V1''
# https://blog.csdn.net/fuxuemingzhu/article/details/79275636
class Solution(object):
def singleNonDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
N = len(nums)
left, right = 0, N - 1 #[left, right]
while (left < right):
mid = left + (right - left) / 2
if mid % 2 == 1: mid -= 1
if nums[mid] != nums[mid + 1]:
right = mid
else:
left = mid + 2
return nums[left]
# V2
# Time: O(logn)
# Space: O(1)
class Solution(object):
def singleNonDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
left, right = 0, len(nums)-1
while left <= right:
mid = left + (right - left) / 2
if not (mid%2 == 0 and mid+1 < len(nums) and \
nums[mid] == nums[mid+1]) and \
not (mid%2 == 1 and nums[mid] == nums[mid-1]):
right = mid-1
else:
left = mid+1
return nums[left]