0941.有效的山脉数组 python3 更新双指针方法

0941.有效的山脉数组 python3 从原方法更新到双指针方法
This commit is contained in:
SianXiaoCHN
2022-03-17 13:38:45 -05:00
parent c78dd2400d
commit a90db6e830

View File

@ -106,22 +106,15 @@ class Solution {
```python
class Solution:
def validMountainArray(self, arr: List[int]) -> bool:
if len(arr) < 3 :
return False
i = 1
flagIncrease = False # 上升
flagDecrease = False # 下降
while i < len(arr) and arr[i-1] < arr[i]:
flagIncrease = True
i += 1
while i < len(arr) and arr[i-1] > arr[i]:
flagDecrease = True
i += 1
return i == len(arr) and flagIncrease and flagDecrease
left, right = 0, len(arr)-1
while left < len(arr)-1 and arr[left+1] > arr[left]:
left += 1
while right > 0 and arr[right-1] > arr[right]:
right -= 1
return left == right and right != 0 and left != len(arr)-1
```