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