941有效的山脉数组

from typing import List
class Solution:
def validMountainArray(self, A: List[int]) -> bool:
# 这道题我是我们需要分两段遍历,第一段从第一个到列表最大值,
# 第二段从列表最大值,到列表最后一个。
# 定义指针索引。
index = 0
# 求出列表长度。
length = len(A)
# 第一段遍历,注意这里,index < length需要写到前面。
while index < length - 1 and A[index + 1] > A[index]:
index += 1
# 判断index 有没有增加,就是判断题中给的第一个表达式。
if index == 0:
return False
# 下边第二段循环和第一段类似的。
index1 = index
while index < length - 1 and A[index + 1] < A[index]:
index += 1
if index == index1 :
return False
# 最后我们需要判断是否到了终点。到了终点说明符合题意,否则就是不符合。
return index == length -1
原文地址:https://www.cnblogs.com/cong12586/p/13921315.html