mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-21 22:51:07 +08:00
0941.有效的山脉数组 python3 更新双指针方法
0941.有效的山脉数组 python3 从原方法更新到双指针方法
This commit is contained in:
@ -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
|
||||
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user