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