From 03582efc4a2e8532d48e70e9bbfad78de430ed65 Mon Sep 17 00:00:00 2001 From: jerryfishcode <91447694+jerryfishcode@users.noreply.github.com> Date: Mon, 27 Sep 2021 18:23:48 +0800 Subject: [PATCH] =?UTF-8?q?Update=200941.=E6=9C=89=E6=95=88=E7=9A=84?= =?UTF-8?q?=E5=B1=B1=E8=84=89=E6=95=B0=E7=BB=84.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0941.有效的山脉数组.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/problems/0941.有效的山脉数组.md b/problems/0941.有效的山脉数组.md index c4c8ebfa..2073ba59 100644 --- a/problems/0941.有效的山脉数组.md +++ b/problems/0941.有效的山脉数组.md @@ -154,6 +154,16 @@ func validMountainArray(arr []int) bool { ## JavaScript ```js +var validMountainArray = function(arr) { + if(arr.length < 3) return false;// 一定不是山脉数组 + let left = 0, right = arr.length - 1;// 双指针 + // 注意防止越界 + while(left < arr.length && arr[left] < arr[left+1]) left++; + while(right>0 && arr[right-1] > arr[right]) right--; + // 如果left或者right都在起始位置,说明不是山峰 + if(left === right && left !== 0 && right !== arr.length - 1) return true; + return false; +}; ``` -----------------------