---
# System prepended metadata

title: 0896. Monotonic Array
tags: [Leetcode, FaceBook, Easy]

---

# 0896. Monotonic Array
###### tags: `Leetcode` `Easy` `FaceBook`
## Code
### 官方解法 强烈推荐 O(N) O(1)
不能用异或门 只能用非门
适用于判断array里面是不是又有A情况又有B情况的题目
```java=
class Solution {
    public boolean isMonotonic(int[] nums) {
        boolean increase = true;
        boolean decrease = true;
        for(int i = 0;i < nums.length-1;i++){
            if(nums[i]<nums[i+1]){
                increase = false;
            }
            else if(nums[i]>nums[i+1]){
                decrease = false;
            }
        }
        // System.out.println(increase+" "+decrease);
        return increase||decrease;
    }
}
```
### 自己解法
```java=
class Solution {
    public boolean isMonotonic(int[] nums) {
        boolean increase = false;
        boolean decide = false;
        if(nums.length==1 || nums.length==2) return true;
        for(int i = 0;i < nums.length-1;i++){
            if(nums[i]!=nums[i+1] && !decide){
                decide = true;
                increase = nums[i]<nums[i+1]?true:false;
            }
            if(decide){
                if(increase && nums[i]>nums[i+1]){
                    return false;
                }
                else if(!increase && nums[i]<nums[i+1]){
                    return false;
                }
            }
        }
        return true;
    }
}
```