class Solution {
public:
vector<bool> checkArithmeticSubarrays(vector<int>& nums, vector<int>& l, vector<int>& r) {
vector<bool> res;
for(int i=0;i<l.size();i++){
// 取出子数组
vector<int> son(nums.begin()+l[i],nums.begin()+r[i]+1);
if(son.size()<=2){
res.push_back(true);
continue;
}
// 检查son是否为等差数组
// 对son排序
sort(son.begin(),son.end());
int d = son[1]-son[0];
int flag = 1;
for(int j=0;j<son.size()-1;j++){
if(son[j+1]-son[j] == d){
continue;
}else{
res.push_back(false);
flag = 0;
break;
}
}
if(flag) res.push_back(true);
}
return res;
}
};
1630. 等差子数组
最新推荐文章于 2024-11-07 23:24:07 发布