思路 ,[0,0,0,0,0,0] [1,2,3,4,5,6] [6,5,4,3,2,1] 都是等差数列 就是判断是否为等差数列
1. 原数组 去重 正序排列
2.递归 获取等差值
3.等差值 去重
4.判断等差值(去重后数组长度分3种情况 0:相同数子, 1:顺序或逆序 ,其他:正确)
let arr = [9,1,7,6,5,4]
class verifications {
constructor() {
this.val = []
}
// 正序排列 去重
_init(val) {
this.val = this.DuplicateRemoval(val.sort(function (a,b) {
return a - b;
}))
}
handle(val) {
this._init(val)
let newVal = this.DuplicateRemoval(this._filters(this.val))
if (newVal.length !== 0 && newVal.length !== 1) {
return true
}else {
return false
}
}
// 递归处理等差数列
_filters(val,newVal = [],index = 0) {
if (index <= val.length - 2) {
newVal.push(val[index + 1]- val[index])
index++
this._filters(val,newVal,index)
}
return newVal
}
// 去重
DuplicateRemoval(val) {
return Array.from(new Set(val))
}
}
let p = new verifications()
console.log(p.handle(arr))