问题:求是否给定的数列为一个山形数列(先单调增,再单调减)
Example 1: Input: [2,1] Output: false Example 2: Input: [3,5,5] Output: false Example 3: Input: [0,3,2,1] Output: true Note: 0 <= A.length <= 10000 0 <= A[i] <= 10000
参考代码:
1 class Solution { 2 public: 3 bool validMountainArray(vector<int>& A) { 4 int status= 1; 5 if(A.size()<3) return false; 6 for(int i=1; i<A.size(); i++){ 7 if(status==1 && A[i]<=A[i-1]) return false; 8 if(status==-1 && A[i]>=A[i-1]) return false; 9 if(i+1<A.size()&&A[i+1]<A[i]&&status==1){ 10 status=-1; 11 } 12 } 13 if(status==-1) return true; 14 else return false; 15 } 16 };