Valid Mountain Array
判断一个数组是不是先增加后减少. 这个我第一次是前后扫做的, 后来看了眼答案, 答案是计数法做的. 就是算增加有多少, 减少有多少, 然后比较是不是全体数都算进去了.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class Solution { public boolean validMountainArray(int[] A) { if(A == null || A.length < 2) return false; int count = 0; while(count < A.length - 1 && A[count] < A[count+1]) count++; if(count == 0 || count == A.length - 1) // no find return false; while(count < A.length - 1 && A[count] > A[count+1]) count++; if(count == A.length-1) return true; else return false; } } |