class Solution {
public:
int numberOfArithmeticSlices(vector<int>& A) {
int N = A.size();
for(int i=N-1;i>0;i--) A[i] -= A[i-1];
int cnt = 2;
queue<int> q;
for(int i=2;i<N;i++){
if(A[i] == A[i-1]){
cnt++;
}else{
if(cnt>2) q.push(cnt);
cnt = 2;
}
}
if(cnt>2) q.push(cnt);
int ans = 0;
while(!q.empty()){
int t = q.front();
q.pop();
ans += (t-2)*(t-1)/2;
}
return ans;
}
};
No.56 - leetcode413 - 等差子串个数
本文介绍了一种高效算法,用于计算数组中等差数列切片的数量。通过一次遍历和使用队列,算法可以快速找出所有长度大于等于3的等差数列切片,为复杂数据结构问题提供了解决方案。

被折叠的 条评论
为什么被折叠?



