problem
solution1:
class Solution { public: bool isMonotonic(vector<int>& A) { int inc = true, dec = true; for(int i=1; i<A.size(); ++i) { inc &= (A[i]>=A[i-1]); dec &= (A[i]<=A[i-1]); } return inc || dec; } };
solution2:
class Solution { public: bool isMonotonic(vector<int>& A) { int inc = false, dec = false; for(int i=1; i<A.size(); ++i) { if(A[i]>A[i-1]) inc = true; if(A[i]<A[i-1]) dec = true; if(inc && dec) return false; } return true; } };
参考
1. Leetcode_easy_896. Monotonic Array;
2. discuss1;
3. discuss2;
完