---
description: https://leetcode.com/problems/monotonic-array/
---

# 896. Monotonic Array

判断数组是否有单调性。遍历并用一个变量记录遇到的单调性，当遇到不满足已记录的单调性的元素时返回false。

```
/*
 * @lc app=leetcode id=896 lang=cpp
 *
 * [896] Monotonic Array
 */

// @lc code=start
class Solution {
public:
    bool isMonotonic(vector<int>& A) {
        if (A.size() <= 1) return true;
        int inc = 0;
        for (int i = 1; i < A.size(); ++i) {
            if (A[i] < A[i - 1]) {
                if (inc == 1) return false;
                inc = -1;
            } else if (A[i] > A[i - 1]) {
                if (inc == -1) return false;
                inc = 1;
            }
        } 
        return true;
    }
};
// @lc code=end


```
