题解 | 寻找峰值 | 逆序遍历
寻找峰值
http://www.nowcoder.com/practice/1af528f68adc4c20bf5d1456eddb080a
寻找索引最大的山峰,从后往前遍历,遇到山峰就返回。
# 寻找最后的山峰
# @param a int整型一维数组
# @return int整型
#
class Solution:
def solve(self , a ):
# write code here
n = len(a)
for i in range(n - 1, -1, -1):
if (i == n - 1 and a[i] >= a[i - 1]) or (i == 0 and a[i] >= a[i + 1]):
return i
if a[i] >= a[i - 1] and a[i] >= a[i + 1]:
return i
return -1