# C#LeetCode刷题之#896-单调数列（Monotonic Array）

1 <= A.length <= 50000
-100000 <= A[i] <= 100000

An array is monotonic if it is either monotone increasing or monotone decreasing.

An array A is monotone increasing if for all i <= j, A[i] <= A[j].  An array A is monotone decreasing if for all i <= j, A[i] >= A[j].

Return true if and only if the given array A is monotonic.

Input: [1,2,2,3]

Output: true

Input: [6,5,4,4]

Output: true

Input: [1,3,2]

Output: false

Input: [1,2,4,5]

Output: true

Input: [1,1,1]

Output: true

Note:

1 <= A.length <= 50000
-100000 <= A[i] <= 100000

```public class Program {

public static void Main(string[] args) {
int[] nums = { 1, 2, 5 };

var res = IsMonotonic(nums);
Console.WriteLine(res);

}

private static bool IsMonotonic(int[] A) {
return isMonotonicIncrease(A) || isMonotonicReduce(A);
}

private static bool isMonotonicIncrease(int[] A) {
for(var i = 0; i < A.Length - 1; i++) {
if(A[i + 1] < A[i]) {
return false;
}
}
return true;
}

private static bool isMonotonicReduce(int[] A) {
for(var i = 0; i < A.Length - 1; i++) {
if(A[i + 1] > A[i]) {
return false;
}
}
return true;
}

}```

`True`