-
Notifications
You must be signed in to change notification settings - Fork 19
/
answer.py
48 lines (42 loc) · 1.35 KB
/
answer.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
#!/usr/bin/env python3
#-------------------------------------------------------------------------------
# Logarithmic Time
#-------------------------------------------------------------------------------
class Solution(object):
def findPeakElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return None
low, high = 0, len(nums) - 1
while low <= high:
if low == high:
return low
mid = (low + high) // 2
if nums[mid] < nums[mid+1]:
low = mid + 1
else:
high = mid
return mid
#-------------------------------------------------------------------------------
# Linear Time
#-------------------------------------------------------------------------------
class Solution(object):
def findPeakElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return None
if len(nums) == 1 or nums[0] > nums[1]:
return 0
for i in range(1, len(nums)-1):
if nums[i-1] < nums[i] > nums[i+1]:
return i
if nums[-1] > nums[-2]:
return len(nums)-1
return None
#-------------------------------------------------------------------------------