Title:Valid Mountain Array 941
Difficulty:Easy
原题leetcode地址:https://leetcode.com/problems/valid-mountain-array/
1. 从左到右递增直到一个较小的数,对这个较大的位置进行判断,然后依次递减
时间复杂度:O(n),两次一层while循环,需要遍历整个数组。
空间复杂度:O(1),没有申请额外空间。
/**
* 从左到右递增直到一个较小的数,对这个较大的位置进行判断,然后依次递减
* @param A
* @return
*/
public static boolean validMountainArray(int[] A) {
int index = 0;
while (index + 1 < A.length && A[index] < A[index + 1]) {
index++;
}
if (index == 0 || index == A.length - 1) {
return false;
}
while (index + 1 < A.length && A[index] > A[index + 1]) {
index++;
}
return index == A.length;
}