题目描述:
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.
Example 1:
Example 2:
Example 3:
Example 4:
Example 5:
Note:
题目大意:
一个单调数组的定义就是数组元素单调递增或者单调递减。
测试样例见题目描述
解题思路:
对于n个元素数组来说判断是否递增/减的方法就是遍历一遍数组元素看看是递增还是递减。
A[i] >= A[i - 1]; // 递增 A[i] <= A[i - 1]; // 递减
参考LeetCode Discuss:
https://leetcode.com/problems/monotonic-array/discuss/165899/1-liner-C++
C++代码: