# 941.Valid Mountain Array

**941.Valid Mountain Array**

题目难度 Easy

> 给定一个整数数组 A，如果它是有效的山脉数组就返回 true，否则返回 false。 让我们回顾一下，如果 A 满足下述条件，那么它是一个山脉数组： A.length >= 3 在 0 < i < A.length - 1 条件下，存在 i 使得： A\[0] < A\[1] < ... A\[i-1] < A\[i] A\[i] > A\[i+1] > ... > A\[B.length - 1]

```
示例 1：

输入：[2,1]
输出：false
示例 2：

输入：[3,5,5]
输出：false
示例 3：

输入：[0,3,2,1]
输出：true


提示：

0 <= A.length <= 10000
0 <= A[i] <= 10000
```

方法：此题与[852](https://github.com/newdee/Leetcode/blob/master/852.Peak_Index_in_A_Mountain_Array.md)有些相似，但题目要求是判断是否是山脉数组，而不是找出山脉。

* 当长度小于3时，直接返回false
* 从开始和结尾来逐个判断，首先从开始判断，当未到达结尾时，如果碰到前面的数比后面的数大，认为此时是第一个山脉，退出循环。
* 如果循环退出后，山脉索引到达了结尾，表示最大值在结尾，返回false
* 同样，从结尾开始判断，如果碰到后面的数比前面的数小，则认为此时是最后一个山脉，退出循环。
* 如果最后一个山脉索引为0，表示是递减数列，返回false；如果最后一个山脉的索引和第一个山脉的索引值相等，返回true。

```
class Solution {
public:
    bool validMountainArray(vector<int>& A) {
        size_t As=A.size();
        if(As <3)
            return false;
        int st=0,et=As-1;
        while(st<et)
        {
            if(A[st]>=A[st+1])
                break;
            st++;
        }
        if(st==et) return false;
        while(et>st)
        {
            if(A[et]>=A[et-1])
                break;
            et--;
        }
        return st==et && st ;

    }
};
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dfine.gitbook.io/leetcode/941.valid_mountain_array.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
