题目链接:896. 单调数列 - 力扣(LeetCode)

题目描述:

可以利用C++库里的is_sorted函数,判断是否是非递减或是非递增数列。时间复杂度:

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
auto init = [](){
ios::sync_with_stdio(false);
cin.tie(nullptr);
return 0;
}();
class Solution {
public:
bool isMonotonic(vector<int>& nums) {
return is_sorted(nums.begin(), nums.end(), [](int &a, int &b){
return a < b;
}) | is_sorted(nums.begin(), nums.end(), [](int &a, int &b){
return a > b;
});
}
};

评测结果: