美文网首页Leetcode刷题笔记
第二十天 Peak Index in a Mountain Ar

第二十天 Peak Index in a Mountain Ar

作者: 业余马拉松选手 | 来源:发表于2018-09-09 00:49 被阅读3次

已经20天了呢

还不算那么难坚持吧

不过今天这道题确实有点太水了

https://leetcode-cn.com/problems/peak-index-in-a-mountain-array/description/

明显感到,后面出的题目的难度和有趣程度都低了很多呢

今天这道题,其实就是求数组中最大的值的索引,不过我还是按照题目的思路来先写了一个解

class Solution:
    def peakIndexInMountainArray(self, A):
        """
        :type A: List[int]
        :rtype: int
        """
        for i in range(1,len(A)-1):
            if A[i] > A[i-1] and A[i] > A[i+1]:
                return i

当然还是有更简单的方法:

class Solution:
    def peakIndexInMountainArray(self, A):
        """
        :type A: List[int]
        :rtype: int
        """
        return A.index(max(A))

语义上理解也更清晰一些吧

嗯,顺便对于Python的list又稍微熟悉了一下

相关文章

网友评论

    本文标题:第二十天 Peak Index in a Mountain Ar

    本文链接:https://www.haomeiwen.com/subject/tihrgftx.html