题目
代码部分一(10ms 12.37%)
class Solution {
public boolean validMountainArray(int[] A) {
if(A.length < 3) return false;
boolean flag = true;
int max = A[0];
int n = 0;
for(int i = 1; i < A.length; i++){
if(max < A[i]){
max = A[i];
n = i;
}
}
int l = n - 1;
int r = n + 1;
if(l < 0 || r >= A.length) return false;
while(flag){
if(l == 0 && r == A.length - 1) flag = false;
if(A[l] >= A[l + 1] || A[l] >= max) return false;
if(A[r] >= A[r - 1] || A[r] >= max) return false;
if(l > 0) l--;
if(r < A.length - 1) r++;
}
return true;
}
}
代码部分二(5ms 94.26%)
class Solution {
public boolean validMountainArray(int[] A) {
if (A.length < 3) return false;
boolean flag = true;
int peak = 0;
for (int i = 1; i < A.length; i ++) {
if (A[i] == A[i - 1]) return false;
if (flag) {
if (A[i] < A[i - 1]) {
flag = false;
peak = i - 1;
}
} else {
if (A[i] > A[i - 1]) {
return false;
}
}
}
return peak > 0 && peak < A.length - 1;
}
}
代码部分三(4ms 100%)
class Solution {
public boolean validMountainArray(int[] A) {
int N = A.length - 1;
int i = 0;
while(i < N && A[i] < A[i + 1]){
i++;
}
if(i == 0 || i == N) return false;
while(i < N && A[i] > A[i + 1]){
i++;
}
if(i != N) return false;
return true;
}
}