题解 | #寻找峰值#
寻找峰值
http://www.nowcoder.com/practice/fcf87540c4f347bcb4cf720b5b350c76
用二分法解,注意mid的取值和left和right的关系
class Solution:
def findPeakElement(self , nums: List[int]) -> int:
# write code here
num_length = len(nums)
left = 0
right = num_length - 1
while left < right:
mid = int((left + right) / 2)
if nums[mid] < nums[mid + 1]:
left = mid + 1
else:
right = mid
return right