LintCode Q75 Find Peak Element in Python

  • Jinhai ZHOU
  • 3 Minutes
  • 2016年11月5日
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution:
#@param A: An integers list.
#@return: return any of peek positions.
def findPeak(self, A):
# write your code here
if len(A) < 3:
return -1
beg = 1
end = len(A) - 2
# peak is between beg and end
while(beg + 1 < end):
mid = (beg + end)/2
if A[mid] < A[mid - 1]:
end = mid
elif A[mid] < A[mid + 1]:
beg = mid
else:
return mid # A[mid] > A[mid - 1] and A[mid] < A[mid + 1]
if A[beg] < A[end]:
return end
else:
return beg
知识共享许可协议
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。