两个指针相向而行
如果左指针满足increasing就一直走
右指针满足decreasing就一直走
最终需要满足它们相等且左指针不在0点(in case of all increasing)
右指针不在最末点(in case of all decreasing)
class Solution {
public boolean validMountainArray(int[] A) {
if (A == null) {
return false;
}
int i = 0;
int j = A.length - 1;
while (i + 1 < A.length && A[i + 1] > A[i]) {
i++;
}
while (j > 0 && A[j - 1] > A[j]) {
j--;
}
return i > 0 && j < A.length - 1 && i == j;
}
}
No comments:
Post a Comment