LeetCode 896. Monotonic Array

题目描述

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:

1
2
Input: [1,2,2,3]
Output: true

Example 2:

1
2
Input: [6,5,4,4]
Output: true

Example 3:

1
2
Input: [1,3,2]
Output: false

Example 4:

1
2
Input: [1,2,4,5]
Output: true

Example 5:

1
2
Input: [1,1,1]
Output: true

Note:

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

题目大意:

一个单调数组的定义就是数组元素单调递增或者单调递减。
测试样例见题目描述

解题思路:

对于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++代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
bool isMonotonic(vector<int>& A) {
int size = A.size();
if ( A[size - 1] > A[0]) {
for (int i = 0; i < size - 1; ++i) {
if (A[i + 1] - A[i] < 0) return false;
}
return true;
}
else {
for (int i = 0; i < size - 1; ++i) {
if (A[i] - A[i + 1] < 0) return false;
}
return true;
}
}
};

------本文结束感谢阅读------
坚持原创技术分享,您的支持将鼓励我继续创作!
显示 Gitment 评论