题目:
代码:
class Solution {
public:
int numberOfArithmeticSlices(vector<int>& A) {
int n=A.size();
if(n<=2) return 0;
int count=0,flag=A[1]-A[0];
int ans=0;
for(int i=0;i<n-1;i++){
if(A[i+1]-A[i]==flag){//这个题没考虑溢出的问题
count++;
}
else{
ans+=count*(count-1)/2;
flag=A[i+1]-A[i];
count=1;
}
}
return ans+count*(count-1)/2;
}
};