题解 | 寻找峰值 | 逆序遍历

寻找峰值

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
全部评论

相关推荐

07-03 16:13
嘉应学院 Python
xiaolihuam...:很明显骗子,如果是hr直接约你面试了,哪用得着内推,如果是员工的话,你得多优秀,一线员工直接加你微信,
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务