LeetCode ~ 941. Effective mountains array

941. effective mountains array

Given an array of integers A, if it is valid Mountains array is returned true, otherwise false.

Let us recall that if A satisfies the following conditions, then it is a mountain array:

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]

Example:

输入:[2,1]
输出:false

输入:[3,5,5]
输出:false

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

prompt

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

Individual analytical thinking

class Solution {
    public boolean validMountainArray(int[] A) {
        // 根据条件可以淘汰为空或者长度小于3的数组
        if(A != null && A.length >= 3){
            // 定义一个指针变量
            int index = 1;
            
            // 循环判断当前元素是否大于前一位元素(山峰左侧,上坡)
            while (index < A.length && A[index] > A[index-1]){
                // 符合条件指针后移继续判断
                index++;
            }
            
            // 判断指针是否到达末位或着前面元素大于当前元素
            if (index >= A.length || index == 1){
                // 不符合山峰形状,返回false
                return false;
            }
            
            // 循环判断剩余元素是否小于前一位元素(山峰右侧,下坡)
            while (index < A.length && A[index] < A[index-1]){
                // 符合条件指针后移继续判断
                index++;
            }
            
            // 判断是否完成遍历
            if (index == A.length){
                // 符合山峰形状,返回true
                return true;
            }
        }
        return false;
    }
}

Present the results

Source: stay button (LeetCode)
link: https://leetcode-cn.com/problems/valid-parentheses

Guess you like

Origin www.cnblogs.com/unrecognized/p/11550327.html