-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode162.py
More file actions
22 lines (22 loc) · 771 Bytes
/
Leetcode162.py
File metadata and controls
22 lines (22 loc) · 771 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution:
def findPeakElement(self, nums: List[int]) -> int:
if len(nums) == 1: return 0
left, right = 0, len(nums)-1
while left <= right:
mid = (left+right) // 2
if mid == 0:
if nums[mid] > nums[mid+1]:
return mid
else:
left = mid+1
elif mid == len(nums)-1:
if nums[mid] > nums[mid-1]:
return mid
else:
right = mid-1
elif nums[mid-1] < nums[mid] and nums[mid] > nums[mid+1]:
return mid
elif nums[mid] > nums[mid-1]:
left = mid+1
elif nums[mid] < nums[mid-1]:
right = mid-1