941. Valid Mountain Array

Given an array A of integers, return true if and only if it is a valid mountain array.

Recall that A is a mountain array if and only if:

  • A.length >= 3
  • There exists some i with 0 < i < A.length - 1 such that:
    • A[0] < A[1] < ... A[i-1] < A[i]
    • A[i] > A[i+1] > ... > A[A.length - 1]
      判断一个数组是不是山峰数组,就是先严格递增然后严格递减,不能相等
      class Solution(object):
          def validMountainArray(self, A):
              """
              :type A: List[int]
              :rtype: bool
              """
              index = 1
              n = len(A)
              while index < n and A[index] > A[index - 1]:
                  index += 1
              if index == 1 or index == n:
                  return False
              while index < n and A[index] < A[index - 1]:
                  index += 1
              return index == n
                          

       

posted @ 2020-07-14 22:46  whatyouthink  阅读(72)  评论(0编辑  收藏  举报