-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidMountainArray.java
More file actions
23 lines (21 loc) · 685 Bytes
/
ValidMountainArray.java
File metadata and controls
23 lines (21 loc) · 685 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package leetcode.array;
/**
* @ClassName ValidMountainArray
* @Description 有效的山脉数组 https://leetcode-cn.com/problems/valid-mountain-array/
* @Author changxuan
* @Date 2020/11/3 下午7:47
**/
public class ValidMountainArray {
public static void main(String[] args) {
System.out.println(validMountainArray(new int[]{2, 0, 2}));
}
public static boolean validMountainArray(int[] A) {
if (A.length < 3) return false;
int i = 0, j = A.length - 1;
while (i + 1 < A.length && A[i] < A[i + 1])
i++;
while (j > 0 && A[j - 1] > A[j])
j--;
return i > 0 && j < A.length - 1 && i == j;
}
}