-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValidMountainArray.java
48 lines (43 loc) · 1.35 KB
/
ValidMountainArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package leetcode;
/**
* ValidMountainArray
* https://leetcode-cn.com/problems/valid-mountain-array/
*
* @since 2020-11-03
*/
public class ValidMountainArray {
public static void main(String[] args) {
ValidMountainArray sol = new ValidMountainArray();
System.out.println(sol.validMountainArray(new int[]{1, 2, 3, 4, 5, 6}));
System.out.println(sol.validMountainArray(new int[]{1, 2, 3, 3, 6, 5}));
System.out.println(sol.validMountainArray(new int[]{1, 2, 3, 4, 6, 5}));
System.out.println(sol.validMountainArray(new int[]{6, 5, 4, 3, 2, 1})); // error
}
public boolean validMountainArray(int[] A) {
if (A == null || A.length < 3) {
return false;
}
boolean isIncrease = true;
int lastValue = -1;
int idx = 0;
for (int value : A) {
if (isIncrease) {
if (value < lastValue) {
if (idx == 1) {
return false;
}
isIncrease = false;
} else if (value == lastValue) {
return false;
}
} else {
if (value >= lastValue) {
return false;
}
}
lastValue = value;
idx++;
}
return !isIncrease;
}
}