From a90db6e830474a70dd445a77cef578627374ca5e Mon Sep 17 00:00:00 2001 From: SianXiaoCHN Date: Thu, 17 Mar 2022 13:38:45 -0500 Subject: [PATCH] =?UTF-8?q?0941.=E6=9C=89=E6=95=88=E7=9A=84=E5=B1=B1?= =?UTF-8?q?=E8=84=89=E6=95=B0=E7=BB=84=20python3=20=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E5=8F=8C=E6=8C=87=E9=92=88=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0941.有效的山脉数组 python3 从原方法更新到双指针方法 --- problems/0941.有效的山脉数组.md | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/problems/0941.有效的山脉数组.md b/problems/0941.有效的山脉数组.md index a2444cc1..4b7a978c 100644 --- a/problems/0941.有效的山脉数组.md +++ b/problems/0941.有效的山脉数组.md @@ -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 ```