// 是否是递增的
isIncrease(arr) {
let inc = true
for (let i = 1; i < arr.length; i++) {
inc &= (arr[i - 1] <= arr[i])
if (!inc) return false
}
return true
}
// 是否是单调的
isIncrease(arr) {
let inc = true, dec = true
for (let i = 1; i < arr.length; i++) {
inc &= (arr[i - 1] <= arr[i])
dec &= (arr[i - 1] >= arr[i])
if (!inc && !dec) return false
}
return true
}
// 还有一种是计算总数判断是否单调
isIncrease(arr) {
let inc = 1, dec = 1, n = arr.length
for (let i = 1; i < n; ++i) {
inc += (arr[i - 1] <= arr[i]);
dec += (arr[i - 1] >= arr[i]);
}
return (inc === n) || (dec === n);
}